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
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
Posted on Nov 15, 2013 8:21 AM