run shell script from applescript with multiple variables

Hi Guys,
I’ve written a shell script that takes several vars; $1 $2 $3 $4
etc…

The first variable is a folderpath. The rest is just text strings.
When I run it in terminal it looks like this:

./scrptname.ksh “/folder/path/to/folder/” “var1” “var2” “var3”

  • and then it runs off like a charm.

My question: how do I do this from applescript? It would be cool for
me, if I could make it as a droplet - so I could the drop the folder
on it - and thereby have the first variable parsed - and then the
applescript could prompt me for the three remaining variables?

Is this possible in applescript?

Thanks a bunch.

Model: Macbook
AppleScript: 2.1.1(81)
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Something like this should work for you…

on open fileList
	set folderPath to quoted form of POSIX path of item 1 of fileList as string
	set i to 0
	set varString to ""
	repeat
		set i to i + 1
		set varBoxResult to (display dialog "Enter variable " & i default answer "" buttons {"Cancel", "Done", "Another"} default button 3)
		set varString to varString & " " & text returned of varBoxResult
		if button returned of varBoxResult is "Done" then exit repeat
	end repeat
	set SC to "/path/to/scrptname.ksh " & folderPath & varString
	--do shell script SC
	display dialog SC
end open

Save it as an application and give it a test run… If the string displayed looks the way you want it too then comment out the display dialog and uncomment the do shell script line and resave.

You should be off to the races.

Hi,
Thanks for your quick reply.

When I run it the script editor says: “TERM environment variable not
set”

  • ??

The as is version is doing this? Or the version that actually runs the shell script?

The do shell script SC

Whats the full command it should be trying to run? When ran through the display does it appear correctly?

It should be important to note that when a AppleScript performs a “do shell” command the commands are ran within the “SH” shell not your default shell (whatever that may be). Perhaps your script needs a bit of tweaking… Be sure to specify full paths to commands.

The fulle command as I run it from the Terminal is:
/Users/hansen/Scripts/htmlgallery.ksh /Users/hansen/Desktop/engangskamera/ “Testevent” “17.02.2007”

If I run your script in the “display mode”, I get:
/Users/hansen/Scripts/htmlgallery.ksh ‘/Users/hansen/Desktop/engangskamera/’ Testevent 17.02.2007

As you can see, it adds quotes to the path.

Any ideas?

well the quotes in the path shouldn’t matter at all… they are there on purpose in case you drop a folder with a space in the name since it won’t escape the space. I see you are putting quotes around your arguments though, so we can try that.

on open fileList
	set folderPath to quoted form of POSIX path of item 1 of fileList as string
	set i to 0
	set varString to ""
	repeat
		set i to i + 1
		set varBoxResult to (display dialog "Enter variable " & i default answer "" buttons {"Cancel", "Done", "Another"} default button 3)
		set varString to varString & " " & quoted form of (text returned of varBoxResult)
		if button returned of varBoxResult is "Done" then exit repeat
	end repeat
	set SC to "/path/to/scrptname.ksh " & folderPath & varString
	--do shell script SC
	display dialog SC
end open

Still the same: “TERM environment variable not
set”

What is your script doing?

A lot!

It determines filetypes, makes image processing, copies pictures online and updates some indexes… almost 700 lines of shell script… It would be so nice if I could just drop a folder on to an icon, so I don’t have to open the terminal…

Okay, well do something simple through AS so you can verify that running a shell command is working

set h to "hello"
do shell script "say " & h

If that works then I would say for some reason something in your shell script is choking.

Yes, that works. Can you give me the syntax for parsing three vars to the script?

The script works fine when I run it from the Terminal.

try this and see if it works

do shell script "/Users/hansen/Scripts/htmlgallery.ksh /Users/hansen/Desktop/engangskamera/ \"Testevent\" \"17.02.2007\""

That will send to the shell the exact syntax that works when you run it from the terminal itself. Lets start with that and see if that works… If it doesn’t then something about your script is bombing when being interpreted by the SH shell.

Accindently i typed a wrong path - and then it said: folder not found. Then I corrected the error, and got:

“TERM environment variable not set”

… damn

For giggles try this…

do shell script "export TERM=xterm-color;/Users/hansen/Scripts/htmlgallery.ksh /Users/hansen/Desktop/engangskamera/ \"Testevent\" \"17.02.2007\""

G R E A T !! - it worked, and I got my output in script editor:

Working in /Users/hansen/Desktop/westside
Determing filetype…
Type is: jpg
There are 10 jpg files in /Users/hansen/Desktop/westside
Working in /Users/hansen/Desktop/westside
Creating directory pages
Directory pages created
Creating directory thumbs
Directory thumbs created
Creating directory orgs
Directory orgs created
Working in /Users/hansen/Desktop/westside
Processing files of type: jpg…
Copying files to dir: orgs
Files copied to directory: orgs
Copying files to dir: thumbs
Files copied to directory: thumbs
Deleting original files
Original files deleted succesfully
Working in /Users/hansen/Desktop/westside
Creating thumbnails from originals…
Thumbnails created

etc etc…

So how do we transfer it in to at droplet?

And actually I only need to vars - besides the folder path.

THANK you.

Only 2 variables? Give this a try

on open fileList
	set folderPath to quoted form of POSIX path of item 1 of fileList as string
	set var1 to quoted form of text returned of (display dialog "Enter value for variable 1" default answer "")
	set var2 to quoted form of text returned of (display dialog "Enter value for variable 2" default answer "")
	do shell script "export TERM=xterm-color;/Users/hansen/Scripts/htmlgallery.ksh " & folderPath & " " & var1 & " " & var2
end open

Wauv - you’re too cool. It worked like a charm. After I dropped the folder onto the application, it just displays the application name in the menubar.

Is there some way to get a processbar - or maybe the balloon - or something?

Thanks again.

Yes & No are the answer. You can’t do a progress bar with Vanilla AppleScript, but you can through either an Xcode project or through the use of a 3rd party progress app that you manipluate… An excelllent example, and one I used for quite a while, is Bruce Phillips Progress Bar - http://scriptbuilders.net/files/bpprogressbar1.0.html

The problem though I forsee is you would have to use a barber pole style indicator since you have the meat of the work being done in a shell script which cant communicate back to the progress bar. A better way perhaps, regardless of which progress approach you take, would be too make your shell script into an AppleScript… even if you end up doing multiple “do shell script” within the AppleScript. This way you could at least put progress updaters in as you see fit.