sashans13 Posted August 27, 2015 Share Posted August 27, 2015 (edited) Hi guys, newbie here. I have trouble passing an argument (usually passed with {query}) to NSAppleScript and I have no idea what I'm doing wrong. I'm trying to make a script that'll grep for me with "grep keyword" in the current finder window folder. However, I'm not able to pass the keyword to NSAppleScript, I'll illustrate on an example: What I type in alfred: "grep test" What I get in Terminal: "grep -inr {query} '/Users/[username_stripped]/Downloads/'" I've tried playing around with it but nothing seems to work, {query} doesn't seem to be evaluated at all. Is this normal behaviour? I've tried googling for a couple of minutes, but no luck. .alfredworkflow: https://www.dropbox.com/s/i3v387z01esrqn4/grep%20finder.alfredworkflow?dl=0 on alfred_script(q) tell application "Finder" set pathList to (quoted form of POSIX path of (folder of the front window as alias)) end tell tell application "System Events" if not (exists (processes where name is "Terminal")) then do shell script "open -a Terminal " & pathList else tell application "Terminal" activate tell application "System Events" to tell process "Terminal.app" to keystroke "t" using command down do script ("grep -inr {query} " & pathList) in first window end tell end if end tell end alfred_script Edited August 27, 2015 by sashans13 Link to comment
RodgerWW Posted August 27, 2015 Share Posted August 27, 2015 If you use "Run Script" and set the language to "usr/bin/osascript" leave the first line in that says: set theQuery to "{query}" and paste in your script, and save ... that should return your 'test' Link to comment
sashans13 Posted August 27, 2015 Author Share Posted August 27, 2015 Thanks for the reply! I tried changing it to a "Run Script" action (using /usr/bin/osascript), but when I do, it doesn't run at all (no errors in the console though). Any ideas? Link to comment
RodgerWW Posted August 27, 2015 Share Posted August 27, 2015 did you connect the keyword to the new Run Script? Link to comment
sashans13 Posted August 27, 2015 Author Share Posted August 27, 2015 I did, here's the code, you may test it: https://www.dropbox.com/s/xx6jc4r9m3rmj7n/grep%20finder2.alfredworkflow?dl=0 Link to comment
RodgerWW Posted August 27, 2015 Share Posted August 27, 2015 remove the line: "on alfred_script(q)" and also "end alfred_script" and it should work Link to comment
sashans13 Posted August 28, 2015 Author Share Posted August 28, 2015 Yeah, that'll do it Thanks Rodger, appreciate it! Link to comment
deanishe Posted August 28, 2015 Share Posted August 28, 2015 FWIW, NSAppleScript works differently to Run Script. There is no "{query}". Instead, the query is the q variable in on alfred_script(q).That is to say, you just needed to change your script to: [...] do script ("grep -inr " & quoted form of q & " " & quoted form of pathList) in first window [...] Note: You should use quoted form of pathList, too, otherwise the script probably won't work for paths with spaces, quotes or apostrophes in them. 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