Jump to content

DRY (do-not-repeat-yourself) Applescript possible?

Recommended Posts

Hi all

Is it possible to link applescript modules together so their variables pass from one to another?

For example in the attached image - the central script module sets-up some variables which are then dealt with differently in the end-of-chain script modules.

I've tried using {var:varname} to pass-through but it's not working...

Is this actually possible?

Here's an example workflow: https://www.dropbox.com/s/j52zibfpui9z0p7/multi-ascript.zip?dl=0



Screen Shot 2017-03-31 at 19.30.22.png

Link to comment

Hi there,


I've moved this to the proper forum. The Discussion & Help forum is for general Alfred usage. This forum is the right place for questions related to creating workflow.


You should start by reading the stickied post on workflow variables. I'm afraid they don't work that way. There are no {var:...} expansions in Run Script elements. You have to use environment variables, e.g. set myVar to (system attribute "MY_VARIABLE")


Your workflow also doesn't actually set any variables. Your central Run Script element (the one selected in your screenshot) is never run. It isn't connected to any kind of trigger or keyword. In order to set variables, you also have to return JSON or XML to Alfred, which isn't super easy in AppleScript. Perhaps better to combine the script with an Args and Vars utility to set the variable from {query}, so you can simply return the variable value from your AppleScript instead of trying to generate XML/JSON.

Edited by deanishe
Link to comment

Unless you need to open the URLs in a specific way, you don't need to use AppleScript to talk to different browsers. You can just use the open command with a URL:

# open in default browser
open https://www.google.com

# open in Safari
open -a Safari https://www.google.com

# open in Chrome
open -a "Google Chrome" https://www.google.com

If that would work for you, then you wouldn't need to use the crazy language that is AppleScript.


Opening a fixed series of URLs can be as simple as this.

Edited by deanishe
I meant that other crazy language
Link to comment



Yeah - what I want is to open certain sites on set days of week or month. Then I can run the action daily and keep track of sites I read etc..

The `copy` command was a fluke find. Works well for this.

This is how I've done it:

set thisDay to weekday of (current date) as string
set thisDate to day of (current date)

set sites to {}

-- Monday
if thisDay is "Monday" then
  copy "https://throttlehq.com/home" to the end of sites

end if

-- Tuesday
if thisDay is "Tuesday" then
  copy "http://twodollartues.com" to the end of sites
end if

-- 1st of each month
if thisDate is 1 then

  copy "http://macappdeals.com" to the end of sites

end if

-- Everyday
copy "http://macupdate.com" to the end of sites

tell application "Firefox"
  repeat with site in sites
    do shell script "/usr/bin/open -a Firefox.app " & site
  end repeat
end tell


Link to comment

Got you. One observation: your do shell script line is technically incorrect. It should be do shell script "/usr/bin/open -a Firefox.app " & (quoted form of site)


Because you're passing the URL to a shell, if it contains any characters with special meaning in a shell ("&" is a common one in URLs), you need to quote the URL or the shell will misinterpret it.


As a rule, any variable you pass to do shell script should use (quoted form of theVariable), as it's liable to die in flames otherwise.

Link to comment

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...