Jump to content

How do I pass two variables to AppleScript?


Recommended Posts

Thanks in advance for the help!

 

I'm trying to pass two (or multiple) variables to AppleScript. For example, “keyword query1 query2” and then get both arguments into AppleScript. Or something like “keyword query1^query2” and then split them. I feel like I’m almost there, but missing something… I guess I should add that I’m using Alfred 3.5.1 with Powerpack. I read this article:

 

 

but my dumb brain couldn’t figure it out… ;) Any help would be greatly appreciated. My use case for this particular workflow is AppleScipt, but I'd be interested in knowing how to do the same thing with a bash script or something like that. 

 

Thanks!

Link to comment
1 hour ago, avclark said:

I feel like I’m almost there, but missing something…

 

The first thing you appear to be missing is that the query you enter into Alfred is one string. Alfred isn't a shell: it doesn't treat spaces as argument delimiters.

 

If you want to turn a two-word query into two arguments, you either need to split it yourself or let bash do it for you with ./myscript.scpt {query}.

 

The second thing is that the query is not a variable. It's passed as a argument to your script, not set as an environment variable.

 

If you need more concrete assistance, upload your workflow somewhere and post a link, so we can run it for ourselves.

Edited by deanishe
Link to comment

Thanks. That totally makes sense. I'm sure I'm using the wrong terminology. The thing I'm uncertain of is how to get from Keyword, to AppleScript, where I would use write:

set x to word 1 of “{query}”

set y to word 2 of “{query}”

 

I'm new to Alfred 3, so I'm still trying to figure out the Args and Vars. For example, I read through this post:

 

 

and one of the replies used this graphic (red highlighting is mine):

 

cTStr6Z.thumb.png.3a9a3464045999241425fc1aa81efba4.png

 

What is going on in the "store input in var project"? What information do I put into the Args and Vars utility to make that happen? 

 

I hope that makes sense. 

 

I would post my workflow, but it doesn't really exist yet. All I have is the Keyword at the beginning and the Run NSAppleScript at the end. My AppleScript is complete and works with one argument. I'm just not sure about the middle part and how to split and store the argument so that my script can use them. 

 

In case I'm not making sense, here's a pseudo code version of what I'm trying to do:

 

Keyword project1^^project2 -> split "project1^^project2" into "project1" and "project2" -> do something in AppleScript with "project1" and "project2" separately, rather than just "project1^^project2" as it is now.

 

Hope that makes more sense. 

 

Thanks.

Edited by avclark
Link to comment
10 minutes ago, avclark said:

What is going on in the "store input in var project"? What information do I put into the Args and Vars utility to make that happen?

 

It's creating a workflow variable with the value of the query entered by the user in the preceding Keyword.

 

argvars.png.0966860a1ac713e60c77a8bfd1e839ad.png

 

13 minutes ago, avclark said:

I would post my workflow, but it doesn't really exist yet.

 

There is far too much room for vagueness and mistakes with screenshots, text descriptions and pseudocode, especially when, as you note, you aren't using the right terminology.

 

These things go much faster and much better when we all have a copy of the workflow we're talking about.

 

Here's a demo workflow that splits user input into two variables.

Link to comment

That's awesome. That's so simple. How do I learn more about this? Like, I would never have reasoned my way to that conclusion. Is there documentation somewhere?

 

Specifically, I'm talking about "theWords", "item 1", "word 1", etc... Is that specific to AppleScript or is that specific to Alfred? Just trying to narrow my search to learn more about this stuff...

 

Thanks again. 

Edited by avclark
Link to comment
9 minutes ago, avclark said:

use other languages to manipulate apps

 

You can't (except JavaScript). But for what you're doing you don' t need to.

 

If you tell iTerm to open a folder, its default behaviour is to create a new tab and cd to the directory. So you can use Alfred's Open File action or /usr/bin/open -a iTerm /path/to/folder.

 

Apart from the odd looney who actually likes AppleScript, most of us write the least possible amount of AppleScript and then call it from a sensible language.

Edited by deanishe
Link to comment

Well, I still know more about Javascript than AppleScript. ;) Mostly I used Alfred in combination with Bash scripts to speed up repetitive web dev tasks. This is first time I've tried to use it to, say, open a new tab in iTerm. But that's good to know. I really appreciate the help!

Link to comment
6 hours ago, deanishe said:

If you want to turn a two-word query into two arguments, you either need to split it yourself or let bash do it for you with ./myscript.scpt {query}.

 

Or set 2 environments variables using 2 chained Keyword objects connected to an Arg and Vars object and accessing them using `(system attribute "myEnvVar")` in AppleScript.

 

@avclark, I made a small workflow to show you 3 options:

  • 1 by using 2 Keyword object that each set an environment variable
  • 1 by using 2 Keyword object where one set an environment variable and the output of the other is directly used as the passed argument to the AppleScript
  • 1 by using 1 Keyword object to write your 2 words separated by a space and which the full string is sent to the AppleScript and where both words are extracted

Link: https://nofile.io/f/owD6a5d5U2g/Example+-+Pass+two+variables+to+AppleScript.alfredworkflow

 

Edited by GuiB
Link to comment

Hey @deanishe I was attempting to go with what appears to be the preferred way and save my script as a file and just call it from within an run script action. The problem is none of my arguments/variables are being passed to the script... The script works fine, but it just ignores the q1 and q2.

 

Here's what my new run script looks like. All the iTerm stuff from before has been abstracted to ht.applescript.

 

5a7d1e29c93b9_ScreenShot2018-02-08at11_04_12PM.png.3d5fe3ecba7de19e5e2b3d56c16ab5a8.png

 

Thanks in advance for the help man!

 

Edited by avclark
Link to comment

@avclark, from the look at your image, you're mixing AppleScripting with Bash scripting. In your image, everything is AppleScript, but you line just before the last one `osascript ~/scripts/ht.applescript` and the Language that your Run Script is set (`/bin/bash`)

 

To run an external AppleScript using a Run Script set into Bash language and passing an argument, just put: `osascript ~/scripts/ht.applescript "$1"`

 

Arguments are passed differently between scripting languages since they have their own syntax. In bash, the first argument can be accessed with $1 (which is used here since alfred pass it's query as one string argument).

 

To start learning, I think you're better building small scripts directly inside Alfred to play around with different scripting languages and start with a default Run Script and switch between languages to see what are the default scripts for each language. You'll see how to catch the arguments into a variable (the default script set a "query" variable to the argument passed).

 

Also, if you want to run a script externally, you would need to make you external script executable by running a terminal command to change its permissions: `chmod +x`

Edited by GuiB
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...