Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

TECK

macrumors 65816
Original poster
Nov 18, 2011
1,129
478
I'm trying to emulate the following pattern:
  • Have several variables defined into Automator script
  • Running the Automator script will ask you what variable to use, from defined list
  • Automator will execute the command every 30min

I tried what shows into screenshot, but it would print the value 1. I do not know how to pass the variable into one liner.

Still, my goal is to have the script ask me what variable I want to use, then execute the command using the chosen variable value every 30 minutes. Is this something feasible? I'm very familiar with shell scripting, cronjobs, launchd .plist, I would like to learn using Automator.

Thank you for your help.

1611332365059.png
 

Red Menace

macrumors 6502a
May 29, 2011
583
230
Colorado, USA
Each time the workflow is run, the variables will be reset to the original settings. You will need to save them to preferences/defaults, or otherwise keep the workflow running. Automator isn’t really the tool to use, you might have better luck with a stay-open AppleScript application using an idle handler or NSTimer.
 
  • Like
Reactions: TECK

Mark FX

macrumors regular
Nov 18, 2011
159
17
West Sussex, UK
As "Red Menace" has pointed out, the Automator application is probably not the right tool for the job.
Because it doesn't offer the ability to repeat the workflow every 30 minutes as you require.
The "Automator" application only offers a "Loop" function, which will loop the workflow x number of times.
Or to loop the workflow repeatedly for x number of minutes.
So a stay open AppleScript is the way to go, something like this very bare bones basic example below.

AppleScript:
on run {}
    idle {}
end run

on open {}
    idle {}
end open

on idle {}
    set commandVariable to choose from list {"alpha", "beta", "gamma"} with prompt "Select the shell command variable to use."
    if commandVariable is equal to false then
        return 1
    else
        set shellCommand to do shell script "echo " & commandVariable
        display dialog "Your chosen shell command variable was '" & shellCommand & "'" buttons {"OK"} default button "OK"
        return 1800
    end if
end idle

on quit {}
    set quitApplication to display dialog "Are you sure you want to quit the application ?" buttons {"No", "Yes"} default button "Yes"
    if button returned of quitApplication is "Yes" then
        continue quit
    end if
end quit

If you copy and paste this code into a new "Script Editor" file and click the run button, you will see how it works.
You will also notice that it will run only once in the Script Editor application, this is because for the idle handler to function, the script needs to be saved as a stay open application, only then will the idle handler repeat with the desired return value, which is the number of seconds between repeat runs of the application.

To save it as a stay open application, go to the "File" menu in Script Editor, and then click the "Save" menu item.
and the usual file save dialog box will be displayed, so give your application a name, I called mine "Shell Runner".
Then you need to click the "File Format:" combo box near the bottom, and select the "Application" option, and then also select the "Stay open after run handler" option box.
This will create the stay open AppleScript application, if you double click this application as usual, it will run like any other application, except the idle handler will repeat with the returned number of seconds.

In the example code above, if you click the "Cancel" button while picking from the list, this returns a "false" boolean value, so the idle handler returns 1 second, which means the choose from list dialog box will display immediately.
But if you choose one of the options from the list, the idle handler will return a value of 1800 seconds, which is the equivalent of the 30 minute you desired, but can be set to any value you want.

The good thing about going the stay open application route, is that you have a full standalone application package.
Which you can right click and view the package contents, which will have a proper "info.plist" file, and an ".icns" icon file that you can change to your own custom icons, along with other options to grow and develop the application.

The way to produce the same result in Automator, is pictured below, but doesn't have the option of of repeating every x number of minutes, but hopefully the picture shows the workflow steps needed to duplicate the above script.

Screenshot.png

In the Automator workflow showed above, I've set the choose from list "Cancel" button to a harmless shell command.
And you can obviously edit the "Run Shell Script" command displayed to anything you want to run.

Hope this information is of some help to you.

Regards Mark
 
Last edited:
  • Like
Reactions: TECK
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.