Subject22 Posted June 8, 2013 Share Posted June 8, 2013 (edited) I'm trying to make a workflow which when invoked by a hotkey will hide the extensions of the files currently selected in OS X but I'm not too sure how Alfred's "Selection in OS X" hotkey action panel works and there seems to be dearth of documentation about it. What exactly does this action panel pass to a script (e.g.: a string)? What is the normal way of receiving this input in a script (e.g.: with "{query}" or with something else)? Does this behaviour vary between different types of scripts (i.e.: bash, AppleScript etc.)? I set up a simple workflow and found that return "{query}" posted a notification as expected when run from the Run Script action panel (with all delimiters unchecked) but not when run from the Run NSApplescript action panel (with the alfred_script(q) wrapper). Why is this? Is there anything else I should know about the "Selection in OS X" hotkey action panel? Edited June 10, 2013 by Subject22 Link to comment
_mk_ Posted June 8, 2013 Share Posted June 8, 2013 The "Selection in OS X" hotkey results in a tab delimitted string containing all selected files with their full path. Have you tried to use the AppleScript with assigning {query} to a variable? set tmpVar to "{query}" return tmpVar Link to comment
Tyler Eich Posted June 8, 2013 Share Posted June 8, 2013 (edited) I'm trying to make a workflow which when invoked by a hotkey will hide the extensions of the files currently selected in OS X but I'm not too sure how Alfred's "Selection in OS X" hotkey action panel works and there seems to be dearth of documentation about it. What exactly does this action panel pass to a script (e.g.: a string)? What is the normal way of receiving this input in a script (e.g.: with "{query}" or with something else)? Does this behaviour vary between different types of scripts (i.e.: bash, AppleScript etc.)? I set up a simple workflow (as shown below) and found that return "{query}" posted a notification as expected when run from the Run Script action panel (with all delimiters unchecked) but not when run from the Run NSApplescript action panel (with the alfred_script(q) wrapper). Why is this? Is there anything else I should know about the "Selection in OS X" hotkey action panel? "{query}" doesnt work in an NSApplescript; the q variable (alfred_script(q)) is what you use. Try replacing "{query}" with q (no quotes). Cheers Edited June 8, 2013 by Tyler Eich Link to comment
jdfwarrior Posted June 8, 2013 Share Posted June 8, 2013 "{query}" doesnt work in an NSApplescript; the q variable (alfred_script(q)) is what you use. Try replacing "{query}" with q (no quotes). Cheers It's not specifically the q variable. That variable can be named whatever you want it to be. The on alfred_script portion is a function that Alfred calls. So you are creating a function that accepts data in the form of a variable and then can use it. So, use whatever variable name you want Link to comment
Tyler Eich Posted June 8, 2013 Share Posted June 8, 2013 It's not specifically the q variable. That variable can be named whatever you want it to be. The on alfred_script portion is a function that Alfred calls. So you are creating a function that accepts data in the form of a variable and then can use it. So, use whatever variable name you want Of course; variable names are irrelevant Just make sure your alfred_script(variable) matches the variable you use in your script. For example, if you used alfred_script(query), make sure you put query everywhere you want the passed text (like 'Selection in OS X') Examples: on alfred_script(q) display dialog q end alfred_script on alfred_script(query) display dialog query end alfred_script on alfred_script(your_variable_name) display dialog your_variable_name end alfred_script Cheers Link to comment
Subject22 Posted June 8, 2013 Author Share Posted June 8, 2013 (edited) Awesome, thanks to you all for clearing that up. I'm sort of picking up AppleScript as I go, learning bits and pieces as I need them and thus far I haven't needed to really know anything about handlers. Now I'm unclear as to how the 'query' variable (or whatever it happens to be named) makes its way into the script. alfred_script() is a function ("handler" in AppleScript parlance) but the variable passed to it appears to be uninitialised. If I did this in some other language it wouldn't work: def someFunction(someVariable): print "I'm doing something" someFunction(query) # query hasn't been initialised, so how can someFunction access it? Additionally, since the 'query' variable is passed to the alfred_script() handler how does the rest of the script end up having access to it? Why isn't this analogous to the following (incorrect) code? def someFunction(someVariable): print "I'm doing something" someFunction(query) print query # Again, the variable hasn't been initialised, how does print access it? EDIT: One other thing, is there some standard method of iterating over files passed into an Applescript with Alfred? The script gets a tab delimited string, so what's the neatest way of iterating over that string, converting each file path to a file that Applescript can work with? This is what I'm currently using and it's kind of nasty. set AppleScript's text item delimiters to " " set filePaths to text items of q repeat with filepath in filePaths set theFile to POSIX file filepath as alias #do stuff end repeat Edited June 9, 2013 by Subject22 Link to comment
Tyler Eich Posted June 9, 2013 Share Posted June 9, 2013 (edited) Awesome, thanks to you all for clearing that up. I'm sort of picking up AppleScript as I go, learning bits and pieces as I need them and thus far I haven't needed to really know anything about handlers. Now I'm unclear as to how the 'query' variable (or whatever it happens to be named) makes its way into the script. alfred_script() is a function ("handler" in AppleScript parlance) but the variable passed to it appears to be uninitialised. If I did this in some other language it wouldn't work: def someFunction(someVariable): print "I'm doing something" someFunction(query) # query hasn't been initialised, so how can someFunction access it? Additionally, since the 'query' variable is passed to the alfred_script() handler how does the rest of the script end up having access to it? Why isn't this analogous to the following (incorrect) code? def someFunction(someVariable): print "I'm doing something" someFunction(query) print query # Again, the variable hasn't been initialised, how does print access it? EDIT: One other thing, is there some standard method of iterating over files passed into an Applescript with Alfred? The script gets a tab delimited string, so what's the neatest way of iterating over that string, converting each file path to a file that Applescript can work with? This is what I'm currently using and it's kind of nasty. set AppleScript's text item delimiters to " " set filePaths to text items of q repeat with filepath in filePaths set theFile to POSIX file filepath as alias #do stuff end repeat I think Alfred initializes the alfred_script(q) handler using whatever is passed to the NSAppleScript object. Kind of like this: # What Alfred's Doing alfred_script("Selection in OS X") # Alfred's handler, found in the NSAppleScript object on alfred_script(q) display dialog q end alfred_script So q is assigned to "Selection in OS X" If you wanted to activate the code outside of Alfred, you would have to call the handler manually and provide some value: (* Given this Handler... *) on alfred_script(q) display dialog q end alfred_script (* ...this would work... *) alfred_script("RED ALERT!") (* ...or this would work... *) set some_text to "Lorem ipsum dolor sit amet" alfred_script(some_text) (* ...or even this *) alfred_script("Hello" & space & "World") As for iterating over a list of files, I can't think of a better method (with AppleScript). AppleScript is great for a lot of things, but it's features/syntax are kind of odd . Edited June 9, 2013 by Tyler Eich Link to comment
Subject22 Posted June 9, 2013 Author Share Posted June 9, 2013 I think Alfred initializes the alfred_script(q) handler using whatever is passed to the NSAppleScript object. So Alfred essentially replaces all instances of "q" with the string passed in from OS X? Is this something that happens as a sort of textual preprocessing? As for iterating over a list of files, I can't think of a better method (with AppleScript). AppleScript is great for a lot of things, but it's features/syntax are kind of odd . OK, guess I've just been spoiled by Python's string handling routines Link to comment
Carlos-Sz Posted June 9, 2013 Share Posted June 9, 2013 Awesome, thanks to you all for clearing that up. I'm sort of picking up AppleScript as I go, learning bits and pieces as I need them and thus far I haven't needed to really know anything about handlers. Now I'm unclear as to how the 'query' variable (or whatever it happens to be named) makes its way into the script. alfred_script() is a function ("handler" in AppleScript parlance) but the variable passed to it appears to be uninitialised. If I did this in some other language it wouldn't work: def someFunction(someVariable): print "I'm doing something" someFunction(query) # query hasn't been initialised, so how can someFunction access it? Additionally, since the 'query' variable is passed to the alfred_script() handler how does the rest of the script end up having access to it? Why isn't this analogous to the following (incorrect) code? def someFunction(someVariable): print "I'm doing something" someFunction(query) print query # Again, the variable hasn't been initialised, how does print access it? EDIT: One other thing, is there some standard method of iterating over files passed into an Applescript with Alfred? The script gets a tab delimited string, so what's the neatest way of iterating over that string, converting each file path to a file that Applescript can work with? This is what I'm currently using and it's kind of nasty. set AppleScript's text item delimiters to " " set filePaths to text items of q repeat with filepath in filePaths set theFile to POSIX file filepath as alias #do stuff end repeat Here is a small change to handle files with AppleScript: set text item delimiters to tab set filePaths to text items of q set text item delimiters to "" repeat with filepath in filePaths set theFile to POSIX file filepath as alias --do stuff end repeat It is a good practice to set the text item delimiter to its default value. Tyler Eich and lendenmc 2 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