Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

New Unix Applescript commands System
The new December 2001 Developer CD provides a couple new applescript commands. The most useful one is do shell script [shell commands as string]. This sends commands to the unix-layer without opening a Terminal window. It will also return the result. So, if you want to get a list of files in your Documents folder in long format into a variable, do the following:

set my_doc_list to {do shell script "ls -al ~/Documents"}

The other new command is POSIX path of [mac path]. Sadly, this only does half the job. It does change the ":" delimiters to "/" and adds the "/Volumes/" directory for mounted volumes, but it does not add the escapes for special characters.

If you'd like an AppleScript soubroutine that finishes the job, read the rest of the article. Note: this still can't handle high-bit characters in the path (option-f for instance).

The following subroutine will add the escapes required for special characters in the path:
on unix_path(mac_path)
set unixpath to (POSIX path of mac_path)
set chars to every character of unixpath
repeat with i from 1 to length of chars
if "!$&\"'*(){[|;<>?~` \\" contains (item i of chars as text) then
set item i of chars to "\\" & (item i of chars as text)
end if
end repeat
return every item of chars as string
end unix_path
  • Currently 4.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)

New Unix Applescript commands | 14 comments | Create New Account
Click here to return to the 'New Unix Applescript commands' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Authored by: kerim on Dec 17, '01 10:57:57AM

This is great - that means that it should be possible to write applescripts to extract e-mail messages from Entourage and decode them with GPG. Right now I have to log in to Terminal in order to encrypt and decrypt messages. I would be very happy if someone with some applescripting experience could suggest how such a script might work. It would need to go through the following process:

(1) save e-mail as a temp document
(2) convert temp document line endings to UNIX
(3) tell GNU gpg to decrypt ("%sudo gpg -o final -d temp")
(4) convert the final document line endings to Mac
(5) open file with text edit.

One difficult part is that passwords will need to be entered . . .

[ Reply to This | # ]
Malicious possibilities?
Authored by: Peganthyrus on Dec 17, '01 11:46:58AM

do shell script "rm -r /*.*"

or something like that?

I <i>do</i> hope that it's not possible to quietly do evil things like that with this new capability.

[ Reply to This | # ]
Malicious possibilities?
Authored by: bombich on Dec 17, '01 06:15:13PM


do shell script "echo 'rootpassword' | sudo -S <malicious code>"

would do the job. Without the root password, though, you can't do it. I wonder what would happen if the script was open by root, i.e. from cron...

[ Reply to This | # ]
Now if only I could download the developer tools...
Authored by: sjonke on Dec 17, '01 01:31:49PM

I hope I'm not the only one that has been trying off and on for days without success. Can't even get the download started, it just sits forever waiting for the "page" to finish loading (i.e. the download to start). At least it isn't giving an error page any more.

[ Reply to This | # ]
Response to two messages
Authored by: james_sorenson on Dec 17, '01 01:54:55PM

First off, about the "rm -r *.*" concern. Errrr...yeh, it is entirely possible. Downloading and running a script can be as dangerous as downloading and running an application. Take into account where you get the script. Truth be told, Applescript already had this ability without the shell script command. Basically, you would tell finder create a home folder with replacing (this overwrites your Home folder with an empty one). It's instantaneous and deadly. Personally, I don't run a script unless:
a) someone else has ran it with success
b) I personally know the source to be a trustworthy one
c) I have looked at the code myself.
Trojans are a nasty breed.

<Sigh> I can't shake the fear that some jerk is gleaming ideas from this thread.

Now, about Downloading the Developers Tools. I found good bandwidth in the early morning hours (7AM or so). I got the Tools in 30 minutes on my DSL account. Six in the evening was completely pointless. I could hardly load the page itself!

[ Reply to This | # ]
Downloading doesn't work in MSIE!
Authored by: sjonke on Dec 17, '01 04:16:45PM

On a whim I decided to try downloading the developer's tools from within OmniWeb 4.1sp14 and, much to my amazement, the download started right away! So I went back to IE and tried it again from within there - as before it looks likes its starting the download but then it never does. I get an endlessly throbbing "E" logo. So I try it from OmniWeb 4.1 on my iBook too and again it works fine! Has anyone else seen this? In any case I'm currently, and quite happily, downloading from OmniWeb 4.1 sneakypeek 14. Need to check to see if there's a newer sneakypeek....

[ Reply to This | # ]
Stupid Geek Log
Authored by: james_sorenson on Dec 17, '01 04:32:52PM

Well, the GeekLog messed up my back-slashes. Here is the REAl applescript for path-conversion:

on unix_path(mac_path)
set unixpath to (POSIX path of mac_path)
set chars to every character of unixpath
repeat with i from 1 to length of chars
if "!$&\"'*(){[|;<>?~` \\" contains (item i of chars as text) then
set item i of chars to "\\" & (item i of chars as text)
end if
end repeat
return every item of chars as string
end unix_path

[ Reply to This | # ]
So where is OS X 10.1.2?
Authored by: sjonke on Dec 17, '01 05:18:38PM

According to the documentation, AppleScript Studio (affectionately known as "ASS") applications can not be run in 10.1.1. They require 10.1.2. If my results with testing out watson (not to be confused with that other Watson) are any indication, this is largely true. So, where is 10.1.2?

[ Reply to This | # ]
A.S.S. and other stuff
Authored by: james_sorenson on Dec 17, '01 06:26:29PM

A.S.S. stuff is different. They are full-on applications with embedded applescript code. This is just straight applescript commands for the Script Editor. The thing is that you either need the Dec 2001 Developer tools OR the upcoming MacOS X update.

Okay folks, I regret responding to the suggestion of malicious code. Keep it to yourself from here on. Let's not turn my hint into a trojan resource, please.

[ Reply to This | # ]
Getting off (my ASS)
Authored by: pmccann on Dec 18, '01 09:54:14AM

Thanks for the applescript James. (aaah, applescript, looks so "English-like", proves so "bloody frustrating-like". While I've done good things with Applescript it has extracted a considerable toll! Tonights mini-episode involved the posted script. To test it I added a "set me to unix_path("space:System Folder") line at the top, and a "display dialog me" at the end. I then puzzled aloud for about two minutes at the "access denied" error message, before remembering that "me" is a reserved word in applescript. )

Anyway, this is not meant to be one of my pathetically negative rambling messages, but distinctly more positive: applescript just became *immensely* more powerful with the shell script capability. Because, for example, perl scripts "are" shell scripts. So as per the above Entourage query, you should be able to use full perl parsing capabilities on your email messages. No more ugly applescript string hacking. (Yes, it's *really* ugly!) Real hashes if you need them. Yum.

In concert with this, you can put very minimal applescript wrappers around, for example, perl scripts, and create GUI apps using ASS. The very thing whose absence I was lamenting no more than a couple of days ago [that is, the inability to run a shell script from applescript except by opening a new terminal window every time]] is now present. There's a nice sample of how to make an ASS (of) yourself via a shell script in

/Developer/Examples/AppleScript Studio/Simple Shell

Yes, Veronica, it *is* simple. Finally, it is true that you cannot deploy ASS applications (yeah, yeah, I actually like the acronym) until 10.1.2 is released, but for local consumption they run fine through projectbuilder.


[ Reply to This | # ]
Adventures in Scriptland
Authored by: vaalrus on Jan 20, '02 10:11:35PM

Your little routine saved me a massive tumor... I suppose I could have tried something a little less involved for my first script, but I need* this to work... A droplet to convert pdb doc files back to raw text..., feeding a list of files to txt2pdbdoc

As follows

on open theFiles
end open

to convertem(whatToChange)

repeat with aFile in whatToChange
set inputfile to (unix_path(aFile))

(do shell script "/usr/local/bin/txt2pdbdoc -d " & inputfile & " " & inputfile & ".txt")

end repeat

end convertem

Works peachy, I even made it a toolbar script...

now I just have to go and add some characters to your routine... thing is, one of my drives has a "Sigma" as part of it's volume name (Old B5 naming scheme, don't ask, long story.) which slips through the filter... my own fault for insisting on carrying over naming patterns from OS 9 (Dag nabit, i want* to use slashes 'n stuff. Shucks.)

[ Reply to This | # ]
Adventures in Scriptland
Authored by: Severian on Feb 17, '02 08:00:52PM

Hmmm, maybe I'm just lost, but I can't seem to find any applescript commands on the december developer CD that I just downloaded. Where are those little rascals hiding?

[ Reply to This | # ]
Adventures in Scriptland
Authored by: james_sorenson on Feb 18, '02 12:20:28AM

After you install the Developer CD, run the Script Editor located in Startup Disk:Applications:AppleScript:Script

Next, go to the menu bar and select File: Open Dictionary. Select "Standard Additions." This will give you the dictionary of most built-in Applescript commands (at least the ones I speak of here). Hope this helps you out!


[ Reply to This | # ]
New Unix Applescript commands
Authored by: Breck on Dec 15, '04 10:23:45AM
It's easier to just use "quoted form of"

do shell script "whatever " & quoted form of (POSIX path of mac_path)

[ Reply to This | # ]