0

I created an Automator workflow that renames iOS Simulator Screenshots as they are added to the Desktop folder. But for the workflow to work, I need to run it myself manually. Is there a way to make the workflow watches the Desktop folder continuously and renames screenshots as they are added like a background service?

This is the workflow I created :

Screenshot of the Workflow

oğuz
  • 145
  • 1
  • 13
  • 1
    A folder action is what you want, you might need to enable them using the **Folder Actions Setup** service. A folder action will have the dropped items passed to it, so you don't need the extra **Find Finder Items**, but note that renaming an item in the watched folder will trigger the folder action again. You would need to move the items to another folder before renaming them. – red_menace Apr 24 '20 at 22:42
  • @red_menace, Looking at the OP's linked screen shot, he's already using a **Folder Action**. You're right he doesn't need to use the **Find Finder Items**, _action_, but he also does not need to move the file before renaming it. I just do not see it as a big deal that the _workflow_ runs twice, it's not going to do anything the second time it runs nor hurt anything in doing so, as it's going to run when any _file/folder_ is placed on the **Desktop**, not just the _Simulator Screen Shot - ..._ ones. – user3439894 Apr 25 '20 at 04:16
  • Edward Mordrake, The linked screen shot, in your OP, shows you are all already using a **Folder Action**, which is a _background service_. If it's not triggering automatically, bring up **Folder Action Setup**. e.g. via **Spotlight** by typing it and _toggle_ the _checkbox_ for **[√] Enable Folder Actions**, as this may help to get it working. I know I've have to do that a few times over the years. Also, please read my comment to red_menace. – user3439894 Apr 25 '20 at 04:16
  • @user3439894 - I saw that the OP was already using a Folder action, which would seem to be what he is looking for. Depending on what is being done (the example workflow is incomplete or filled in when run), re-triggering a folder action can be problematic, so generally it is best to avoid that situation. – red_menace Apr 25 '20 at 11:33
  • @red_menace, Generally speaking, if a folder action does not cause an endless loop there is no need to worry about a single re-trigger when it will not have any consequence in doing so! In the case of the OP's workflow it only causes a single re-trigger if a target exists and does so without consequence! It is essentially no different then all the times it will trigger and do nothing when files are created/dropped on the Desktop without the target name. There is absolutely nothing to worry about in this particular use case and the target file(s) do not need to be moved before being renamed! – user3439894 Apr 25 '20 at 14:22
  • @user3439894 - In this particular case, you are correct. Where you can run into issues is for example adding a prefix (without checking) which will wind up with a few more than expected. I was just bringing up something to watch out for. – red_menace Apr 25 '20 at 15:06

2 Answers2

1

I find automator to be a a bit clunky at times, so I suggest you do this with a standard AppleScript Folder Action. Copy the following script into Script Editor.app, modify the prefixes as needed, then save it in ~/Library/Scripts/Folder Action Scripts.

property old_prefix : "Simulator Screen Shot - "
property new_prefix : "some text "

on adding folder items to this_folder after receiving these_items
    repeat with this_item in these_items
        set item_path to POSIX path of this_item
        tell application "System Events"
            set this_disk_item to disk item item_path
            tell this_disk_item
                if its name begins with old_prefix then
                    set its_extention to its name extension
                    set its_name to its displayed name
                    set new_file_name to new_prefix & (text ((length of old_prefix) + 1) through -1 of its_name)
                    if new_file_name ≠ its displayed name and new_file_name ≠ "" then
                        set name of this_disk_item to new_file_name & "." & its_extention
                    end if
                end if
            end tell
        end tell
    end repeat
end adding folder items to

Use Spotlight to open the app "Folder Actions Setup", add your desktop as a folder on the left side, and on the right, select and attach the script you just saved to that folder. It should 'just work'.

Ted Wrigley
  • 2,921
  • 2
  • 7
  • 17
0

The way I would do it is to use an application called launchd to open the automator script how ever often you need (e.g. every few seconds). This website describes how to set it up. Basically you have to first save your automator script as an application into a certain folder (e.g. Documents), then write a short xml script which tells launchd which file to open and how often to do it.

Below is a sample script:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>Label</key>
        <string>com.NAME.test</string>
        <key>Program</key> 
        <string>/Users/USERNAME/Documents/test.app/Contents/MacOS/Application Stub</string>
        <key>StartInterval</key>
        <integer>5</integer>
        <key>RunAtLoad</key>
        <true/>
    </dict>
</plist>

Open a new "Plain text" TextEdit document and paste in the above. Replace the Label (com.NAME.test) with whatever you want; I usually just put my name in there, followed by the name of the script. Next change the line under Program to the location of your ".app" file you saved earlier, remembering to change USERNAME to your username. Keep in mind that the /Contents/MacOS/Application Stub needs to be right after the ".app" part so that the script will start your application. Then change the line under StartInterval to the number of seconds you want the script to wait before it runs again.

After you're done editing the script, save it to "/Users/USERNAME/Library/LaunchAgents/com.NAME.test.plist," of course changing USERNAME to your username and com.NAME.test to the Label used in the xml script. If it asks if you want to save it with the ".plist" extension, choose yes. Once the file's saved, open up Terminal (/Applications/Utilities/Terminal.app) and type in the command launchctl load /Users/USERNAME/Library/LaunchAgents/com.NAME.test.plist, changing the file name to the filepath of your ".plist" file. Use unload instead of load to stop the script from running.

For me a gear icon kept appearing in the menu bar every time the script ran, so I found on this and this website that you can stop it by adding "Run Shell Script" to the very top of your Automator script, then typing in the box killall ScriptMonitor || true.

htmlcat
  • 336
  • 3
  • 10