Apple Script, step up to answer a dumb question

hypertron

Registered
alright, i was using applescript and i wanted to make a script to delete a file in the applications folder. I soon found out it didn't work the same way as terminal (specifying directories just by typing the user's directory, then a backslash, and then continuing until you got to the file), you instead had to specify the directory by using the "set directory to" command. So my first line of code looked like this .
Code:
set directory to "Applications"
It didn't give me any error when i tested it, but when i tried to tell finder to move a certain file to the trash, it gave me an error telling it couldn't fine the file. Here is that code
Code:
tell application "Finder"
	move "TuxRacer" to trash
end tell

Then I had Finder empty the trash, which i had already tested and worked.
Code:
tell application "Finder"
	empty trash
end tell
So all the code together was
Code:
set directory to "Applications"

tell application "Finder"
	move "TuxRacer" to trash
end tell

tell application "Finder"
	empty trash
end tell

So if any one can help that would be great. Also -I know this is the most retarded question ever, but- can someone tell what terminal puts in place of spaces in file names.
Thanks in advance

-Hypertron
 
With respect to 'TuxRacer', you know where it is; but 'Finder' does not.
If 'TuxRacer' is in your 'Applications' folder, and I will use 'Macintosh HD' as your boot drives name, then you must replace "TuxRacer" above with the applications full path (relative to the volume it is on) ...

"Macintosh HD:Applications:TuxRacer"

... or ...

"Macintosh HD:Applications:TuxRacer.app"

----

To answer the UNIX question ('... can someone tell what Terminal [actually, UNIX] puts in place of spaces in file names'), if a file or folder name has a space, such as in 'Macintosh HD', one needs to add a \' before the space, to present its UNIX equivalent; thus 'Macintosh HD' would be written as 'Macintosh\ HD'.
Note. in UNIX the boot drive is designated with by the first '/'; thus "Macintosh HD:Applications:TuxRacer" would be '/Applications/TuxRacer' (in UNIX lingo).

----

The corrected AppleScript code would then be:

tell application "Finder"
delete "Macintosh HD:Applications:TuxRacer"
empty trash
end tell

----

In 'Terminal' you enter:

rm -rf /Applications/TuxRacer.app

... and press the <return> key. Or just enter ...

rm -rf

... add a space after '-rf', drag the 'TuxRacer' application file onto the 'Terminal' window (where the correct path will be automatically generated), and press the <return> key.
 
You don't specify directories in AppleScript like you do in Terminal. When you say set directory to "Applications", all you're doing is creating a new variable called directory, and giving it the string value Applications. Obviously that's not what you want! :eek:

There are several ways to do this. The simplest is to specify the full path of the file (Mac-style, not Unix-style like in Terminal), like so:
Code:
tell application "Finder"
	move item "your_startup_volume_name:Applications:TuxRacer" to the trash
	empty trash
end tell
Where "your_startup_volume_name" is...well, I guess you get the idea. :)

Don't want to have to specify the name of your startup volume? You can go about it another way, too:
Code:
tell application "Finder"
	move item "Applications:TuxRacer" of the startup disk to the trash
	empty trash
end tell
As you can see, in AppleScript, you can access a file by specifying the Mac-style path, or by specifying a subpath (or just the name) qualified by "of such-and-such". You could even say item "TuxRacer" of folder "Applications" of the startup disk if you wanted (but why would you?!).

Edit: Ah, I see barhar beat me by a bit. Oh well.
 
thank you guys so much. Im still trying to wrap my young mind around applescript.
Thanks alot.
More stupid questions to come. (unfortunately)
 
Back
Top