Mac AppleScript help: file exists in user directory?

Oats

macrumors regular
Original poster
Jan 8, 2003
192
1
New York
i am trying to do some a simple applescript, but i could use some help. what i want to do is:

open the folder: ~/Documents/
check if file exists in that folder named "index.html"
if it exists, then delete the file.
if it does not exist, do something else.
then open the parent folder (looking for a generic command similar to "cd ..")

i have tried to figure this out with the "record script" option, but it always uses my current user documents folder, and i would like it to be more generic. thanks if anyone can help!
 

kainjow

Moderator emeritus
Jun 15, 2000
7,745
3
Here's something to get you started:

Code:
[b][color=blue]set[/color][/b] [color=green]docsFolder[/color] [b][color=blue]to[/color][/b] ([color=blue]path to[/color] [color=blue]documents folder[/color] [color=blue]as[/color] [color=blue]Unicode text[/color])
[b][color=blue]set[/color][/b] [color=green]aFile[/color] [b][color=blue]to[/color][/b] [color=green]docsFolder[/color] & "index.html"
[b][color=blue]tell[/color][/b] [color=blue]application[/color] "Finder"
     [b][color=blue]if[/color][/b] [color=blue]exists[/color] [color=blue]file[/color] [color=green]aFile[/color] [b][color=blue]then[/color][/b]
          [color=blue]delete[/color] [color=blue]file[/color] [color=green]aFile[/color]
     [b][color=blue]else[/color][/b]
          --[i][color=olive] do something else[/color][/i]
     [b][color=blue]end[/color][/b] [b][color=blue]if[/color][/b]
     
     [color=blue]open[/color] [color=blue]folder[/color] [color=green]docsFolder[/color]
[b][color=blue]end[/color][/b] [b][color=blue]tell[/color][/b]
 

Eraserhead

macrumors G4
Nov 3, 2005
10,300
10,437
UK
For what its worth, you can use do shell script("insert shell script here") in Applescript to run shell scripts.
 

Oats

macrumors regular
Original poster
Jan 8, 2003
192
1
New York
thanks for the help!
i tried this, but it doesn't work:
Code:
set docsFolder to ("~/Documents")
i guess the "~" is a shell shortcut which applescript doesn't recognize? also the next thing i want to do it the equivalent of "~/Documents/../" . this might suggest i use the shell command... only problem is, with the shell command, how do i do something only if a file exists?
 

kainjow

Moderator emeritus
Jun 15, 2000
7,745
3
thanks for the help!
i tried this, but it doesn't work:
Code:
set docsFolder to ("~/Documents")
i guess the "~" is a shell shortcut which applescript doesn't recognize? also the next thing i want to do it the equivalent of "~/Documents/../" . this might suggest i use the shell command... only problem is, with the shell command, how do i do something only if a file exists?
You are thinking in terms of shell scripting, not AppleScript. AppleScript can do some of this stuff, but it'd be easier to do it in a shell script (using Bash):

Code:
#!/bin/bash
DOCSDIR="${HOME}/Documents"
FILE="${DOCSDIR}/index.html"
if [ -e $FILE ]; then
	rm $FILE
#else
	# do something else
fi
open $DOCSDIR
Save it as myfile.command, then in Terminal run "chmod 755 <path-to-myfile.command>" and then you can double-click and it will open in Terminal and run.

this page has lots more info on bash scripting.