perrybucsdad

Q: How to delete a file with applescript

I am trying to do something really simple, and I am having a heck of a time with it.  I am familiar with VBA and just made the switch over to Apple a year ago.  I am now dabbling with Applescript, but I can't figure out how to do the most basic of things... delete a file.

 

I have a file located in /Users/John/Documents/Scripts names "test.txt". I wrote the following script,

 

tell application "Finder"

          delete (every item of "/Users/John/Documents/Scripts" of disk "Macintosh HD" whose name is "test.txt")

end tell

 

but when executed, it says: " Can’t get "/Users/John/Documents/Scripts" of disk "Macintosh HD". Access not allowed."

 

What am I doing wrong?  This is my user account, and I am logged in as me when I run the script.  I would assume it is a permissions issue, but why would that crop up if I am running this as me?

 

Any help would greatly be appreciated.

 

Also, what is a good book (or web resource) to pick up to help me learn AppleScript?  I have the O'Reilly's Applescript The Definitive Guide, and I feel it is somewhat lacking.  Also, it only goes up through Tiger and I have OSX Mavericks.  I'm not sure if the programing has changed that much between Tiger and Mavericks, but I would like to at least get one written for Lion or Mountain Lion.

 

Thanks,

 

John

MacBook Pro with Retina display, OS X Mavericks (10.9), Issues with Mavericks and upgrades

Posted on Nov 15, 2013 6:36 AM

Close

Q: How to delete a file with applescript

  • All replies
  • Helpful answers

  • by Niel,Helpful

    Niel Niel Nov 15, 2013 7:53 AM in response to perrybucsdad
    Level 10 (311,923 points)
    Nov 15, 2013 7:53 AM in response to perrybucsdad

    Here:

     

    tell application "Finder"

    delete (every item of folder "Documents:Scripts" of home whose name is "test.txt")

    end tell

     

    Using a text string where an explicit file/folder path is expected is poor form; it may work in some places, but doesn't work here, and shouldn't be relied on.

     

    (92756)

  • by Pierre L.,Helpful

    Pierre L. Pierre L. Nov 15, 2013 8:06 AM in response to perrybucsdad
    Level 5 (4,484 points)
    Nov 15, 2013 8:06 AM in response to perrybucsdad

    perrybucsdad wrote:

     

    […]

     

    Also, what is a good book (or web resource) to pick up to help me learn AppleScript?  I have the O'Reilly's Applescript The Definitive Guide, and I feel it is somewhat lacking.  Also, it only goes up through Tiger and I have OSX Mavericks.  I'm not sure if the programing has changed that much between Tiger and Mavericks, but I would like to at least get one written for Lion or Mountain Lion.

     

    See Starting out with AppleScript.

  • by perrybucsdad,Solvedanswer

    perrybucsdad perrybucsdad Nov 15, 2013 8:21 AM in response to Niel
    Level 1 (0 points)
    Nov 15, 2013 8:21 AM in response to Niel

    Thanks!! that works perfectly.  I guess the reason for the explicit path is that is what I am used to with VBA.

     

    Do I need to be concerned with if the file does not exist?  It should always exist, as a file gets written in the next line of code, but because of corruption issues (most likely from me not knowing how to overwrite the actual file) I found that I must delete the file.  To be safe, should I write some sore of IF EXISTS command to test if it is there?  I'm not sure how I would do that with the Tell block.

     

    BTW, my write block is as follows...

     

    -- Write to file

    set myLogFile to open for access POSIX file "/Users/John/Documents/Scripts/test.txt" with write permission

    write OutputString & return to myLogFile

    close access myLogFile

  • by perrybucsdad,

    perrybucsdad perrybucsdad Nov 15, 2013 8:22 AM in response to Pierre L.
    Level 1 (0 points)
    Nov 15, 2013 8:22 AM in response to Pierre L.

    Pierre L. wrote:

     

    See Starting out with AppleScript.

     

    Awesome!! Thank you!!!

  • by Niel,

    Niel Niel Nov 15, 2013 8:25 AM in response to perrybucsdad
    Level 10 (311,923 points)
    Nov 15, 2013 8:25 AM in response to perrybucsdad

    If the file doesn't exist, that line of code won't do anything.

     

    (92761)

  • by Frank Caggiano,

    Frank Caggiano Frank Caggiano Nov 15, 2013 8:43 AM in response to perrybucsdad
    Level 7 (25,782 points)
    Nov 15, 2013 8:43 AM in response to perrybucsdad
    Do I need to be concerned with if the file does not exist?

     

    The open for write will create the file if it does not exist.

     

    You could get errors in the open or in the write if for example you don;t have permission to access the file or permission to create it or if the write fails because of a disk full condition for example. So it is always a good idea to wrap these in a try error block.

     

    One other thing if you run the script out of the editor and the file gets opened but for some reason the close is not executed the nest time you try to run the script you will get an error when you try to open it. If this happens you need to close and then reopen the Applescript editor.

     

    BTW your first example failed because you were mixing POSIX paths and HFS paths. You need to use one or the other and convert POSIX to HFS for things like Finder.

     

    Message was edited by: Frank Caggiano

     

    You should look at  Apple's Applescript Languge Guide You might need to create a free developer account to access that.

  • by perrybucsdad,

    perrybucsdad perrybucsdad Nov 15, 2013 9:00 AM in response to Frank Caggiano
    Level 1 (0 points)
    Nov 15, 2013 9:00 AM in response to Frank Caggiano

    Frank Caggiano wrote:

     

    The open for write will create the file if it does not exist.

     

    But does it overwrite it if it does exist?  I found that if the file existed and had say 3 lines of text and I was writing two lines of text now, it would write the two new lines of text on top, and leave the third line of text behnd.

     

     

    Frank Caggiano wrote:

     

    BTW your first example failed because you were mixing POSIX paths and HFS paths. You need to use one or the other and convert POSIX to HFS for things like Finder.

     

     

    I'm not sure what you mean here...

  • by Frank Caggiano,

    Frank Caggiano Frank Caggiano Nov 15, 2013 9:21 AM in response to perrybucsdad
    Level 7 (25,782 points)
    Nov 15, 2013 9:21 AM in response to perrybucsdad
    But does it overwrite it if it does exist?

     

    From the Applescript Language  Guide - open for access:

     

    Calling open for access on a file returns an integer, termed a file descriptor, which represents an open communication channel to the file’s data. This file descriptor remains open until the script calls close access on it (or on the same file). Each file descriptor maintains a file pointer, which marks the current position within the file and is initially set to the beginning of the file. read and write commands begin reading or writing at the file pointer, unless instructed otherwise using a from or starting at parameter, and advance the file pointer by the number of bytes read or written, so the next operation will begin where the previous one left off.

  • by perrybucsdad,

    perrybucsdad perrybucsdad Nov 15, 2013 9:31 AM in response to Frank Caggiano
    Level 1 (0 points)
    Nov 15, 2013 9:31 AM in response to Frank Caggiano

    Okay, so that makes sense based on what I was seeing.

     

    Now going back to the POSIX vs HFS way of writing things... I'm having a real hard time with this.  I'd like to write the file with the way I wrote the delete statement (not sure which is HDF vs POSIX... trying to find something that compares the two). 

     

    So if I delare two variables in the begining of the scripts for the Folder Path and the File Name as such:

     

    set varFile to "Days.txt"

    set varPath to "Documents:Scripts"

     

    Would I write the Write statement like this (doesn't work)

     

    set myLogFile to open for access file of folder varPath of home whos name is varFile with write permission

     

    ???

  • by Hiroto,

    Hiroto Hiroto Nov 15, 2013 9:39 AM in response to perrybucsdad
    Level 5 (7,281 points)
    Nov 15, 2013 9:39 AM in response to perrybucsdad

    Hello

     

    You can use "set eof" command to clear the existing contents of file. Also you can use "starting at" parameter of "write" command to specify where to start writing in the file, especially when used with "eof" enumeration as in "starting at eof", you can append data to the end of existing contents.

     

    And it is good practice to make sure to close the file handle you opened.

     

    Here's sample code using a write_to_file() handler.

     

    --set f to "/Users/John/Documents/Scripts/test.txt"
    --set f to (path to documents folder from user domain)'s POSIX path & "Scripts/test.txt"
    set f to (path to desktop)'s POSIX path & "test.txt"
    set x to "hello" & return
    
    write_to_file(x, f, {_append:false, _class:«class utf8»})
    
    on write_to_file(x, f, {_append:_append, _class:_class})
        (*
            anything x : object to be written
            string f : POSIX path of output file
            boolean _append : true = append, fales = overwrite
            type class _class : the class as which x is written to file
        *)
        try
            set fh to open for access POSIX file f with write permission
            if not _append then set eof fh to 0
            write x as _class to fh starting at eof
            close access fh
        on error errs number errn from erob
            try
                close access POSIX file f
            end try
            error errs number errn from erob
        end try
    end write_to_file
    

     

    Regards,

    H

  • by perrybucsdad,

    perrybucsdad perrybucsdad Nov 15, 2013 9:43 AM in response to perrybucsdad
    Level 1 (0 points)
    Nov 15, 2013 9:43 AM in response to perrybucsdad

    Okay, I think I figured it out... I'm sure this could be written better, but this works for me...

     

    set varFile to "Days.txt"  -- Define Log File

    set varPath to "Documents:Scripts:" -- Define Path

     

    set f to (path to current user folder as string) & varPath & varFile

    set myLogFile to open for access file f with write permission

    write "Testing" & return to myLogFile

    close access myLogFile


  • by twtwtw,

    twtwtw twtwtw Nov 15, 2013 9:43 AM in response to perrybucsdad
    Level 5 (4,935 points)
    Nov 15, 2013 9:43 AM in response to perrybucsdad

    perrybucsdad wrote:


    But does it overwrite it if it does exist?  I found that if the file existed and had say 3 lines of text and I was writing two lines of text now, it would write the two new lines of text on top, and leave the third line of text behnd.

     

    When you add to a file, the file will place the file pointer at the end of the file and remember it at next access. If you want to overwrite, set the end of file to 0:

     

    set eof of myLogFile to 0

  • by perrybucsdad,

    perrybucsdad perrybucsdad Nov 15, 2013 9:50 AM in response to twtwtw
    Level 1 (0 points)
    Nov 15, 2013 9:50 AM in response to twtwtw

    twtwtw wrote:

     

    perrybucsdad wrote:


    But does it overwrite it if it does exist?  I found that if the file existed and had say 3 lines of text and I was writing two lines of text now, it would write the two new lines of text on top, and leave the third line of text behnd.

     

    When you add to a file, the file will place the file pointer at the end of the file and remember it at next access. If you want to overwrite, set the end of file to 0:

     

    set eof of myLogFile to 0

     

    When?  Meaning, do I do the set eof before I close access to the file, or as the first step of my write command?

  • by perrybucsdad,

    perrybucsdad perrybucsdad Nov 15, 2013 10:52 AM in response to perrybucsdad
    Level 1 (0 points)
    Nov 15, 2013 10:52 AM in response to perrybucsdad

    Never mind... trial and error led me to find that I put the set eof just before the write command.

     

    For anyone who stumbles on this, this is what I have that is working for me...

     

    set varFile to "Days.txt"  -- Define Log File

    set varPath to "Documents:Scripts:" -- Define Path

     

    set f to (path to current user folder as string) & varPath & varFile

    set myLogFile to open for access file f with write permission

    set eof of myLogFile to 0

    write "Testing" & return to myLogFile

    close access myLogFile

     

    Thanks again everyone for all the help... this is hard wrapping my head around this after working in a windows programing environment for 15 years.