tell application "Finder"
set theSelection to the selection
repeat with anItem in theSelection
set thePath to quoted form of POSIX path of (anItem as text)
end repeat
end tell
but where I get thePath, i want to join then next item in the list to it.
so eventually it will look like this file1 file2 file 3… etc.
Its so that the next line is do shell script "rm -rf " & file1 file2 file3 file 4 etc…
set AppleScript's text item delimiters to " "
tell application "Finder"
set theSelection to the selection as alias list
end tell
if theSelection ≠{} then
repeat with anItem in theSelection
set contents of anItem to quoted form of POSIX path of anItem
end repeat
end if
theSelection as text
Stephan’s question is a legitimate one.
You can also directly delete a file using System Events.
set f to "~/Downloads/Evernote_401298_copy.zip"
tell application "System Events"
delete file f
end tell
You can also manipulate (copy, move, delete) an alias list:
tell application "Finder"
set _sel to selection as alias list
if _sel ≠{} then
delete _sel
empty trash
end if
end tell
set {TID, text item delimiters} to {text item delimiters, "' '"}
tell application "Finder" to set theSelection to "'" & POSIX path of (get selection as text) & "'"
set text item delimiters to TID
but it will fail if any path contains a single quote
Yeap the single quote is stopping me at the moment
set {TID, text item delimiters} to {text item delimiters, "' '"}
tell application "Finder" to set theSelection to "'" & POSIX path of (get selection as text) & "'"
set text item delimiters to TID
theSelection as text
do shell script "rm -rf " & theSelection
This is the heavy dutiy solution, feel free to meddle with it.
on posixPathOfAnytingAsText(anyFileOrFolderPath)
-- returns the full posix pathname of anything if the files exists
local tids, theFile, lastItem, tidsToUse, singleQuoteCheck -- Thanks to Yvan Koenig :)
set singleQuoteCheck to false
set tidsToUse to ":"
try
(get class of anyFileOrFolderPath)
on error number -1728 -- it was a filereference
set anyFileOrFolderPath to POSIX path of (anyFileOrFolderPath as alias) as text
return anyFileOrFolderPath
end try
set anyFileOrFolderPath to "" & anyFileOrFolderPath -- doesn't harm.
if anyFileOrFolderPath starts with "'" and anyFileOrFolderPath ends with "'" then
set anyFileOrFolderPath to text 2 thru -2 of anyFileOrFolderPath
set singleQuoteCheck to true
end if
if anyFileOrFolderPath starts with "/" and singleQuteCheck is false then
return anyFileOrFolderPath
else if anyFileOrFolderPath starts with "/" or anyFileOrFolderPath starts with "~" then
set tidsToUse to "/"
end if
set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, tidsToUse}
set anyFileOrFolderPath to text items of anyFileOrFolderPath as text
if singleQuoteCheck is true then
set AppleScript's text item delimiters to "'\\''"
set anyFileOrFolderPath to text items of anyFileOrFolderPath -- as list
set AppleScript's text item delimiters to "'"
end if
set anyFileOrFolderPath to "" & anyFileOrFolderPath
set AppleScript's text item delimiters to tidsToUse
if tidsToUse is ":" then
-- the first item isn' like disk nr 1 then we have to do something.
if text item 1 of anyFileOrFolderPath is not item 1 of (list disks) then
set anyFileOrFolderPath to {"Volumes"} & text items of anyFileOrFolderPath
else
set anyFileOrFolderPath to {""} & text items 2 thru -1 of anyFileOrFolderPath
end if
set AppleScript's text item delimiters to "/"
else if text item 1 of anyFileOrFolderPath is "~" then
-- mus get the posix path as text
set anyFileOrFolderPath to text items 2 thru -2 of POSIX path of (path to home folder) & text items 2 thru -1 of anyFileOrFolderPath
end if
set anyFileOrFolderPath to "" & anyFileOrFolderPath
set AppleScript's text item delimiters to tids
return anyFileOrFolderPath
end posixPathOfAnytingAsText
(You’ll have to enable the singleQuoteCheck for it to work.
This works no matter what character are used in the path:
#!/usr/bin/osascript
#get selection
tell application "Finder"
#use a delimiter that can't conflict with file names; use an forbidden character
set {oldTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/"}
set sel to every text item of (selection as text)
set AppleScript's text item delimiters to oldTID
end tell
#Convert HFS paths into quoted UFS paths
repeat with thePath in sel
set contents of thePath to quoted form of POSIX path of thePath
end repeat
#Convert AppleScript list into a Bash array
set {oldTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space}
set thePaths to sel as string
set AppleScript's text item delimiters to oldTID
do shell script "rm -Rf " & thePaths
tell application "Finder" to set theSelection to selection
if theSelection = {} then return
set args to {"rm", "-rf"}
repeat with anItem in theSelection
set end of args to quoted form of POSIX path of (anItem as text)
end repeat
set {TID, text item delimiters} to {text item delimiters, space}
set shellCommand to args as text
set text item delimiters to TID
do shell script shellCommand
While that is technically correct the speed difference of getting Finder-object-specifiers vs getting an alias list is huge when dealing with more than a very few files.
Is there any data printed to stderr? You can redirect stderr like &2>1 in your command so it will be send to stdout and will appear in your result screen. Or you can remove the -f option from rm and see if there is any error that occurs.