Jump to content
avclark

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!

Share this post


Link to post
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

Share this post


Link to post

if it’s just a single script and single word queries it’s relatively easy - e.g.

 

set x to word 1 of “{query}”

set y to word 2 of “{query}”

Share this post


Link to post

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

Share this post


Link to post
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.

Share this post


Link to post

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

Share this post


Link to post

AppleScript.

 

And the best advice I can give you is: Don't use AppleScript. It's a stupid language.

 

Alfred supports plenty of other languages out of the box, and they're all better than AppleScript. Even PHP.

Share this post


Link to post
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

Share this post


Link to post

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!

Share this post


Link to post
3 minutes ago, avclark said:

I still know more about Javascript than AppleScript

 

Unfortunately, the JS translation of the AppleScript API really sucks. It's not like "real" JavaScript at all :( 

Share this post


Link to post
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

Share this post


Link to post

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

Share this post


Link to post

@avclark In that screenshot, you have the script type set as bash, and the script you've entered is (mostly) AppleScript. Bash will probably be throwing errors for each line other than:

 

osascript ~/scripts/ht.applescript

 

...and this one you haven't passed any arguments to.

Share this post


Link to post

@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

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...