ASLG unix scripts?


Can anyone tell me what this means from the AppleScriptLanguageGuide.pdf:

mainly the this allows you to … part. Because I can’t believe it is true.



Is it unbelieveable because you can have osascript as bash scripts on systems from Snow Leopard onwards, or that it breaks on versions before that? :slight_smile:

(I am pretty sure Apple introduced this feature with Snow Leopard, but maybe it was Leopard, definately not with Tiger.)

Look for shebang or hashbang.

In short: When a file’s flag is executable and started by bash, the program loader will look if the content of the file begins with “#!”, named a shebang. When a file begins with a shebang the rest of the line is the command to the interpreter (including arguments), the file is send as the last argument and that command will be exectuted.

The profit of this over starting it the interpreter by hand is that the script will run in your process list as it’s own process, instead of seeing the name of the interpreter. The drawback is that this doesn’t work with osascript :frowning: So the script is a plain applescript file, with it’s executable flag set (like bash scripts) and will call the interpreter to compile and execute the source code. Still useful creating scripts on the fly from databases as we use that for boot scripts.

So create a text file with this content

[code]#! /usr/bin/osascript -s s

tell application “Finder” to display dialog “hello world”[/code]
now save the file and set it’s executable flag to true (chmod +x /path/to/file), now double click the executable from the finder and voila :slight_smile:


I think you should also be able to execute the osascript by “. theosascript” or “source theosascript”. -Then you execute it in the current process.

I guess there is something with the licensing of bash that makes Apple still use version 3.2.5.

Double clicking on the bash script just opened it in TextEdit. I had to run it from Terminal which works. Nice though. You don’t need to deal with quoting.

Then the executable flag is not correctly set.

Hi DJ Bazzie Wazzie,

Saved the script:

as text file “”. Then ran this from Terminal:

Double clicked the file and it opens in Xcode? With .txt extension it opens in TextEdit.


Hello kel.

The reason that is opens up in XCode, and that this happens when using the extension .sh, is becuase that extension, as a matter of fact overrides uti, except for when a file has an uti set by the user. (I think that is the rules.) You’ll see what program will open it in the File information dialog.

And one more thing, if you aren’t absolutely sure that a shell script file is run, by double clicking it, and you are not absolutely sure about its contents, then you’d better open it in an editor first.

I never double click shell scripts in the finder, I always right click! :slight_smile:

Hi McUsr,

I’ve tried running shell scripts by double clicking, but never could get it to work. What application do I change Xcode to in the info window?


Hi McUsr,

That’s ok. I switched it to I was thinking that there must be a background only shell script runner. I can always make my own background only AppleScript shell script runner if I ever need to.

Thanks a lot,


I used to have it like that, that I executed shell scripts, by double clicking, but I found it to be spooky when I accidentally double clicked something I really didn’t know what was.

Some installers at least in past, managed to put files around. (from zip”archieves).

An easy way to deal with uti’s and file extensions, is to download RCDefaultApps preference pane. Yup! Still works. :slight_smile:

Hi McUsr,

Think I’ve seen shell scripts that I didn’t know where they came from.

I’m wondering why launch services doesn’t list Terminal as a recommended application.

Have a good day,

Come to think of it, I think you’re right. Changed it back to Xcode.


For the record: My shell scripts doesn’t have any extensions. I prefer to open directly in terminal, you can always drag and drop it to your favorite editor.

That’s true. You can drag to an editor and you cannot drag to Terminal. Think I’ll change my mind and switch back to Terminal. :smiley:

Thanks a lot,

Good day,

I’ve been trying something all morning, but I keep getting errors in the shell scripts with double quotes.

Think I’ve finally found why. This has smart quotes. Could that be the reason why it is returning an error in Terminal when I open it? The error is this:


Darn, that was it. Wasted 2 hours. :frowning:

Having fun with parallel AppleScirpts. Not sure what’s going on, but the last dialog shows a list. Here’s the script runner (droplet):

-- list of references
on open params
	-- change to posix paths
	set pp_list to {}
	repeat with this_script in params
		set end of pp_list to quoted form of (POSIX path of this_script)
	end repeat
	-- change list of paths to text
	set tids to AppleScript's text item delimiters
	set AppleScript's text item delimiters to " & "
	set procs to (pp_list as string)
	set AppleScript's text item delimiters to ""
	-- make parallel commands
	set cmd to "(" & procs & " )"
	do shell script cmd
	display dialog result
end open

Here’s some example bash scripts to drop on the droplet:

That -s s option is useful. Didn’t know about that.