Jump to content

Argument passing to NSAppleScript

Recommended Posts

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
			tell application "Terminal"
				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 by sashans13
Link to post

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 post

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...