avclark Posted February 3, 2018 Share Posted February 3, 2018 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
deanishe Posted February 3, 2018 Share Posted February 3, 2018 (edited) 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 February 3, 2018 by deanishe Link to comment
dfay Posted February 3, 2018 Share Posted February 3, 2018 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}” Link to comment
avclark Posted February 3, 2018 Author Share Posted February 3, 2018 (edited) 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): 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 February 3, 2018 by avclark Link to comment
deanishe Posted February 3, 2018 Share Posted February 3, 2018 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. 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
avclark Posted February 3, 2018 Author Share Posted February 3, 2018 Thanks @deanishe. That was exactly what I was looking for. However, the problem I'm running into now is that neither word can have a dash in it... Is there a simple workaround for that or is it a whole different thing? Here's a link to the workflow as it stands right now. Link to comment
deanishe Posted February 4, 2018 Share Posted February 4, 2018 Instead of using word 1 of q, do it this way: set theWords to q's text items set q1 to item 1 of theWords set q2 to item 2 of theWords Link to comment
avclark Posted February 4, 2018 Author Share Posted February 4, 2018 (edited) 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 February 4, 2018 by avclark Link to comment
deanishe Posted February 4, 2018 Share Posted February 4, 2018 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. Link to comment
avclark Posted February 4, 2018 Author Share Posted February 4, 2018 Cool. I didn't know you could use other languages to manipulate apps... I'll have to look into that. Link to comment
deanishe Posted February 4, 2018 Share Posted February 4, 2018 (edited) 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 February 4, 2018 by deanishe Link to comment
avclark Posted February 4, 2018 Author Share Posted February 4, 2018 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
deanishe Posted February 4, 2018 Share Posted February 4, 2018 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 Link to comment
GuiB Posted February 4, 2018 Share Posted February 4, 2018 (edited) 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 February 4, 2018 by GuiB Link to comment
avclark Posted February 4, 2018 Author Share Posted February 4, 2018 Thanks @GuiB. That's super helpful! Link to comment
avclark Posted February 9, 2018 Author Share Posted February 9, 2018 (edited) 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. Thanks in advance for the help man! Edited February 9, 2018 by avclark Link to comment
deanishe Posted February 9, 2018 Share Posted February 9, 2018 Can you upload the/a broken workflow somewhere so I can have a look at it? Link to comment
Andrew Posted February 9, 2018 Share Posted February 9, 2018 @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. Link to comment
GuiB Posted February 9, 2018 Share Posted February 9, 2018 (edited) @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 February 9, 2018 by GuiB Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now