Jump to content

Open File, Find Text, Replace Text, Save File, Close File


Recommended Posts

Posted

First Hi to the forum, I am really looking forward to learning from you guys. 

I have just purchased the power pack in the hope i can achieve the following as a workflow.  First Backstory, the file I am working with is a XML File and this file keeps crashing chrome it is a known bug . 

So I am hoping via a workflow I can

 

a. locate Projectname_Core.XML file
 

b. Find text   
 

<action name="mainloadsceneStartup">
    loadscene(%1, get(projection_current_params), MERGE, get(ptblendmode));
  </action>
 
c. Replace the above text with this text 

<action name="mainloadsceneStartup">
    loadscene(%1, get(projection_current_params), MERGE);
  </action>


d. Save File
 
e. Close File
 
 
As i have many different ongoing projects and all of them require that i make the above  code change to prevent chrome crashing. 

Can this be achieved ion Alfred?


Cheers Simon

 

Posted

This has basically nothing to do with Alfred. You need to edit XML and Alfred is not an XML editor.
 
Alfred can start your program or script for you, but you need to find or write that program first.
 
FWIW, it appears to be a simple enough script to write. It's impossible to say without knowing more about the task, however (such as having a complete XML document for a start).
 
To replace the XML, you'd do something like this in Python (this code is completely untested; it most likely contains errors): 

from xml.etree import ElementTree as ET
 
# The file to update
xml_path = '/path/to/file.xml'
 
# Parse the file into an element tree
tree = ET.parse(xml_path)
root = tree.getroot()
 
changed = False  # Flag we'll set if we change anything
 
# Find all <action name="mainloadsceneStartup"> tags
for elem in root.findall(".//action[@name='mainloadsceneStartup']"):
    # Ensure tag's contents are what we want to replace
    if elem.text = 'loadscene(%1, get(projection_current_params), MERGE, get(ptblendmode));':
        # Update the element's text contents and set flag to save the edited document
        elem.text = 'loadscene(%1, get(projection_current_params), MERGE);'
        changed = True
 
if changed:  # Save file if it was updated
    with open(xml_path, 'wb') as fp:
        # Default XML header
        fp.write('<?xml version="1.0" encoding="utf-8"?>\n')
        # Turn tree back into XML
        fp.write(ET.tostring(root).encode('utf-8'))
Posted

Hi Deanishe

 

Many thanks for you efforts, kindness and support.  I installed Alfred for the first time yesterday, i am a total newbie to Alfred and creating workflows. 

The XML file that I have to open, find text, replace text, save file, close file, is a XML file, so when the text the replacement text is added it prevents chrome from crashing.  The editor I am using to open xml file to these tasks by hand is Sublime Text Editor.  I have created the exact folder structure and added the xml file to the last folder and then uploaded to dropbox.

https://www.dropbox.com/s/co8s07gmvmtmthw/Documents.zip?dl=0

 

I would be extremely happy to pay you for your assistance. To achieve this workflow would be phenominal and  save me lots of frustrating time making these edits by hand. 

Many thanks in advance and once again Thank you. 

Posted

Here is a rudimentary workflow that should do what you want.
 
Unfortunately, I couldn't test it properly because you uploaded the edited XML file, not the original one.
 
The work is done by the fixup.py script in the workflow. It's configurable via command-line options, so you can update other tags instead, but it defaults to the values you specified.
 
The workflow exposes the script as a File Action for XML files, called Fix Core XML
 
The File Action accepts multiple files, so you could do a Finder search for XML files, select the ones you want, open them in Alfred, and update them all at once.

 

If that helps, feel free to hit the "Buy me a beer" link below ;)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...