• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Really delete a file via AppleScript
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Really delete a file via AppleScript


  • Subject: Re: Really delete a file via AppleScript
  • From: Neil Faiman <email@hidden>
  • Date: Sun, 3 Aug 2003 11:47:00 -0400

On Sunday, August 3, 2003, at 08:48 AM, Paul Skinner wrote:

Both look like 'pure' AppleScript to me. And while Michelle's is exactly what you asked for, I suspect in practice that the DSS (do shell script) version is easier used. The method using 'empty the trash' should probably move all the contents of the trash to a temp folder outside of the trash, delete the file you want deleted, and then restore the previous contents of the trash. It's rude to empty people's trash without asking.

PS

Thanks to all for the suggestions (and the confirmation that there wasn't an easy solution that I had missed). Paul Skinner zeroed in on my objection to "delete file; empty the trash" -- that is, that the user (possibly me) might not want their trash emptied.

That leaves the "do shell script" solution. The main problem with this one is that the name of the file that I want to delete might contain shell metacharacters, and "POSIX path of" doesn't provide any quoting. For example, if I had files "AAA", "BBB", and "AAA BBB" in a directory, and selected file "AAA BBB" and passed it to my script, and my script just did

do shell script "rm " and POSIX path of theFile

then the shell would end up doing "rm AAA BBB". Naive quoting would work better in this case:

do shell script "rm \"" and POSIX path of theFile & "\""

but now suppose that I have files 'AAA', 'BBB', and 'AAA" "BBB'. (That latter one is a file whose name is AAA followed by a double quote, a space, another double quote, and BBB.) Then I would end up doing 'rm "AAA" "BBB"', with the same sad results.

All this means that I need to manually quote the path name before I generate the shell command string. The best that I've been able to come up with is shown below. (I know that I could do the string substitutions more easily with various custom scripting additions, but I'm trying to come up with a portable solution.)

Any other comments are welcome -- especially comments pointing out some easy way of quoting an arbitrary string so that it will be accepted as a single shell command argument (or pointing out that I forgot to backslash some other important metacharacter).

Thanks,

Neil Faiman


get the POSIX path of (the file as alias)
replace_text(the result, "\\", "\\\\")
replace_text(the result, "$", "\\$")
replace_text(the result, "\"", "\\\"")
do shell script "rm \"" & the result & "\""

-- Split a string into a list of substrings, breaking on every occurrence of a delimiter string.

to split_string(theString, theDelimiter)
set saveATID to AppleScript's text item delimiters
set AppleScript's text item delimiters to theDelimiter
set theList to every text item of theString
set AppleScript's text item delimiters to saveATID
return theList
end split_string

-- Join a list of strings into a single string, separating them with a delimiter.

to join_strings(theList, theDelimiter)
set saveATID to AppleScript's text item delimiters
set AppleScript's text item delimiters to theDelimiter
set theString to theList as string
set AppleScript's text item delimiters to saveATID
return theString
end join_strings

-- Replace every occurrence of some string in another string with a third string.

to replace_text(theString, textToReplace, theReplacement)
split_string(theString, textToReplace)
join_strings(the result, theReplacement)
end replace_text
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Really delete a file via AppleScript
      • From: David Durkee <email@hidden>
    • Re: Really delete a file via AppleScript
      • From: Michelle Steiner <email@hidden>
References: 
 >Re: Really delete a file via AppleScript (From: Paul Skinner <email@hidden>)

  • Prev by Date: Re: Really delete a file via AppleScript
  • Next by Date: Re: Really delete a file via AppleScript
  • Previous by thread: Re: Really delete a file via AppleScript
  • Next by thread: Re: Really delete a file via AppleScript
  • Index(es):
    • Date
    • Thread