AppleScript help: file exists in user directory?

Discussion in 'Mac Programming' started by Oats, Aug 12, 2007.

  1. macrumors regular

    Joined:
    Jan 8, 2003
    Messages:
    192
    Location:
    New York
    #1
    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!
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    Messages:
    7,743
    #2
    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]
     
  3. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Messages:
    10,275
    Location:
    UK
    #3
    For what its worth, you can use do shell script("insert shell script here") in Applescript to run shell scripts.
     
  4. macrumors regular

    Joined:
    Jan 8, 2003
    Messages:
    192
    Location:
    New York
    #4
    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?
     
  5. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Messages:
    10,275
    Location:
    UK
    #5
    You need to switch between OS X and Unix paths to do it, see this for how.
     
  6. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    Messages:
    7,743
    #6
    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.
     
  7. macrumors regular

    Joined:
    Jan 8, 2003
    Messages:
    192
    Location:
    New York
    #7
    thanks so much! you guys are the best.
     

Share This Page