Create a string of files from repeat

This is what I have so far

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…

Hi,

why not


tell application "Finder"
	delete selection
	empty trash
end tell

Something like this?

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

or


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

I’m using it to delete some items in the trash

as alias list is not needed in this case. selection returns a list of Finder object specifiers or an empty list

Hello.

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.

POSIX path is always text

getting lost now, I originally had it set to delete the file then the next in the repeat, so every time issuing the command, Rm rf…

But when trying to delete a large selection from the trash. it take a while, I thought maybe issue the rm rf the once might do it quicker.

Hello. I should probably revise it a little. :slight_smile:

. and as the class of Finder files is predictable the script is overkill

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

try this


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.

Still not removing the file(s)
this is a simple line. I think the ’ is still causing it to do nothing?

do shell script “rm -rf ‘/Users/StudioA/.Trash/DLY3859WH.psd’”

I have tested mine and it removes files, but Stefan’s is pretty much the same :expressionless:

edit: It must be something else like privilege problems. Even when the file name contains an single quote, Stefan’s script works fine on my machine.

Just given this one a try too, and nothing?

the reply window shows this

tell application “Finder”
get selection
→ {“StudioA:Users:StudioA:.Trash:IZ1137BK_2.psd”, “StudioA:Users:StudioA:.Trash:GYJ211530BKWH.psd”, “StudioA:Users:StudioA:.Trash:URBE000703WH_2.psd”, “StudioA:Users:StudioA:.Trash:URBE000667WH_2.psd”, “StudioA:Users:StudioA:.Trash:DLY81008040BK.psd”, “StudioA:Users:StudioA:.Trash:DLY81008040BR_2.psd”}
end tell
tell current application
do shell script “rm -Rf ‘/Users/StudioA/.Trash/DLY81008040BR_2.psd’”
→ “”
end tell
Result:
“”

but the files remain, same if I try it on files on the desktop.

Hello

Try changing the rm command to “rm -vrf”, at least it may be a little bit more verbose in its feedback to you.

tell application “Finder”
get selection
→ {“StudioA:Users:StudioA:.Trash:IZ1137BK_2.psd”, “StudioA:Users:StudioA:.Trash:GYJ211530BKWH.psd”, “StudioA:Users:StudioA:.Trash:URBE000703WH_2.psd”, “StudioA:Users:StudioA:.Trash:URBE000667WH_2.psd”, “StudioA:Users:StudioA:.Trash:DLY81008040BK.psd”}
end tell
tell current application
do shell script “rm -vrf ‘/Users/StudioA/.Trash/IZ1137BK_2.psd’ ‘/Users/StudioA/.Trash/GYJ211530BKWH.psd’ ‘/Users/StudioA/.Trash/URBE000703WH_2.psd’ ‘/Users/StudioA/.Trash/URBE000667WH_2.psd’ ‘/Users/StudioA/.Trash/DLY81008040BK.psd’”
→ “/Users/StudioA/.Trash/IZ1137BK_2.psd
/Users/StudioA/.Trash/GYJ211530BKWH.psd
/Users/StudioA/.Trash/URBE000703WH_2.psd
/Users/StudioA/.Trash/URBE000667WH_2.psd
/Users/StudioA/.Trash/DLY81008040BK.psd”
end tell
Result:
“/Users/StudioA/.Trash/IZ1137BK_2.psd
/Users/StudioA/.Trash/GYJ211530BKWH.psd
/Users/StudioA/.Trash/URBE000703WH_2.psd
/Users/StudioA/.Trash/URBE000667WH_2.psd
/Users/StudioA/.Trash/DLY81008040BK.psd”

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.