File path problem...

Hello,

Sorry but I really don’t see how to make the following script work. I don’t understand how to get my file path in the correct type so I can delete it. I get the file in “Finder” path and I need it as a POSIX path I presume…

Can someone please help me with this?

Thanks in advance. Here is the problematic sript:


set counterVariable to 0
set stopValue to 20
set stepValue to -1

set AliasDossierSauvegarde to alias "Macintosh HD:Users:andrew:Documents:Scripts:"
set DossierSauvegarde to "Macintosh HD:Users:andrew:Documents:Scripts:"

tell application "Finder"
	set ListeFichiers to sort (every item of AliasDossierSauvegarde whose name extension is "txt") by creation date -- most recent first
end tell

set startValue to count items in ListeFichiers

if startValue > 20 then
	repeat with counterVariable from startValue to stopValue by stepValue
		set theFile to (item -1 of ListeFichiers)
		set theFileText to quoted form of POSIX path of theFile
		-- set theinfo to creation date of (info for theFile) as text
		-- delete theFile
		do shell script "rm " & quoted form of POSIX path of theFileText
	end repeat
end if

Hi Andrew,

There are a few problems:
¢ You have to coerce the Finder file specifier to alias or text before coercing it to POSIX path.
¢ The double coercion to POSIX path will cause an error
¢ You should set the stop value to zero because you’re counting backwards


repeat with counterVariable from startValue to stopValue by stepValue
		set theFile to (item -1 of ListeFichiers)
		do shell script "rm " & quoted form of POSIX path of (theFile as alias)
	end repeat

Hello,

I tried your modifications but I still get an error! This is the error I get:

Can’t make «class docf» “Bidon - copie 25.txt” of «class cfol» “Scripts” of «class cfol» “Documents” of «class cfol» “andrew” of «class cfol» “Users” of «class sdsk» of application “Finder” into type alias.

Here is the script that creates this error:


set counterVariable to 0
set stopValue to 20
set stepValue to -1

set AliasDossierSauvegarde to alias "Macintosh HD:Users:andrew:Documents:Scripts:"

tell application "Finder"
	set ListeFichiers to sort (every item of AliasDossierSauvegarde whose name extension is "txt") by creation date -- most recent first
end tell

set startValue to count items in ListeFichiers

if startValue > 20 then
	repeat with counterVariable from startValue to stopValue by stepValue
		set theFile to (item -1 of ListeFichiers)
		do shell script "rm " & quoted form of POSIX path of (theFile as alias)
	end repeat
end if

I count backwards because I want to keep the 20 most recent files. I need to delete all the older ones!

Thanks for your help.

Andrew

I have no idea why the alias coercion doesn’t work. It should!
Try it with as text


.
do shell script "rm " & quoted form of POSIX path of (theFile as text)
.

Hi,

I also get an error:

Can’t make «class docf» “Bidon - copie 25.txt” of «class cfol» “Scripts” of «class cfol» “Documents” of «class cfol» “andrew” of «class cfol» “Users” of «class sdsk» of application “Finder” into type Unicode text.

That’s my problem, I’m turning in circles with these path types…

Do you have any other suggestion?
Does it work in your environment?

Thanks.

Andrew

Yes ist does.
Normally AppleScript can coerce a Finder file specifier to alias or text even without a Finder tell block

Well, does anyone have another suggestion? Because I am stuck.

Thanks…

why not


tell application "Finder"
	set ListeFichiers to sort (every file of AliasDossierSauvegarde whose name extension is "txt") by creation date -- most recent first
	if (count ListeFichiers) > 20 then delete items 21 thru -1 of ListeFichiers
end tell

I still get an error:

Can’t make {«class docf» “Bidon - copie 1.txt” of «class cfol» “Scripts” of «class cfol» “Documents” of «class cfol» “andrew” of «class cfol» “Users” of «class sdsk» of application “Finder”, «class docf» “Bidon - copie 2.txt” of «class cfol» “Scripts” of «class cfol» “Documents” of «class cfol» “andrew” of «class cfol» “Users” of «class sdsk» of application “Finder”, «class docf» “Bidon - copie 3.txt” of «class cfol» “Scripts” of «class cfol» “Documents” of «class cfol» “andrew” of «class cfol» “Users” of «class sdsk» of application “Finder”, «class docf» “Bidon - copie 4.txt” of «class cfol» “Scripts” of «class cfol» “Documents” of «class cfol» “andrew” of «class cfol» “Users” of «class sdsk» of application “Finder”, «class docf» “Bidon - copie 5.txt” of «class cfol» “Scripts” of «class cfol» “Documents” of «class cfol» “andrew” of «class cfol» “Users” of «class sdsk» of application “Finder”, «class docf» “Bidon - copie 6.txt” of «class cfol» “Scripts” of «class cfol» “Documents” of «class cfol» “andrew” of «class cfol» “Users” of «class sdsk» of application “Finder”, «class docf» “Bidon - copie 7.txt” of «class cfol» “Scripts” of «class cfol» “Documents” of «class cfol» “andrew” of «class cfol» “Users” of «class sdsk» of application “Finder”, «class docf» “Bidon - copie 8.txt” of «class cfol» “Scripts” of «class cfol» “Documents” of «class cfol» “andrew” of «class cfol» “Users” of «class sdsk» of application “Finder”, «class docf» “Bidon - copie 9.txt” of «class cfol» “Scripts” of «class cfol» “Documents” of «class cfol» “andrew” of «class cfol» “Users” of «class sdsk» of application “Finder”, «class docf» “Bidon - copie 10.txt” of «class cfol» “Scripts” of «class cfol» “Documents” of «class cfol» “andrew” of «class cfol» “Users” of «class sdsk» of application “Finder”, «class docf» “Bidon - copie 11.txt” of «class cfol» “Scripts” of «class cfol» “Documents” of «class cfol» “andrew” of «class cfol» “Users” of «class sdsk» of application “Finder”, «class docf» “Bidon - copie 12.txt” of «class cfol» “Scripts” of «class cfol» “Documents” of «class cfol» “andrew” of «class cfol» “Users” of «class sdsk» of application “Finder”, «class docf» “Bidon - copie 13.txt” of «class cfol» “Scripts” of «class cfol» “Documents” of «class cfol» “andrew” of «class cfol» “Users” of «class sdsk» of application “Finder”, «class docf» “Bidon - copie 14.txt” of «class cfol» “Scripts” of «class cfol» “Documents” of «class cfol» “andrew” of «class cfol» “Users” of «class sdsk» of application “Finder”, «class docf» “Bidon - copie 15.txt” of «class cfol» “Scripts” of «class cfol» “Documents” of «class cfol» “andrew” of «class cfol» “Users” of «class sdsk» of application “Finder”, «class docf» “Bidon - copie 16.txt” of «class cfol» “Scripts” of «class cfol» “Documents” of «class cfol» “andrew” of «class cfol» “Users” of «class sdsk» of application “Finder”, «class docf» “Bidon - copie 17.txt” of «class cfol» “Scripts” of «class cfol» “Documents” of «class cfol» “andrew” of «class cfol» “Users” of «class sdsk» of application “Finder”, «class docf» “Bidon - copie 18.txt” of «class cfol» “Scripts” of «class cfol» “Documents” of «class cfol» “andrew” of «class cfol» “Users” of «class sdsk» of application “Finder”, «class docf» “Bidon - copie 19.txt” of «class cfol» “Scripts” of «class cfol» “Documents” of «class cfol» “andrew” of «class cfol» “Users” of «class sdsk» of application “Finder”, «class docf» “Bidon - copie 20.txt” of «class cfol» “Scripts” of «class cfol» “Documents” of «class cfol» “andrew” of «class cfol» “Users” of «class sdsk» of application “Finder”, «class docf» “Bidon - copie 21.txt” of «class cfol» “Scripts” of «class cfol» “Documents” of «class cfol» “andrew” of «class cfol» “Users” of «class sdsk» of application “Finder”, «class docf» “Bidon - copie 22.txt” of «class cfol» “Scripts” of «class cfol» “Documents” of «class cfol» “andrew” of «class cfol» “Users” of «class sdsk» of application “Finder”, «class docf» “Bidon - copie 23.txt” of «class cfol» “Scripts” of «class cfol» “Documents” of «class cfol» “andrew” of «class cfol» “Users” of «class sdsk» of application “Finder”, «class docf» “Bidon - copie 24.txt” of «class cfol» “Scripts” of «class cfol» “Documents” of «class cfol» “andrew” of «class cfol» “Users” of «class sdsk» of application “Finder”} into type number.

Andrew

sorry, I edited the script above

Thanks a lot.

Your solution works just fine.

Have a nice day or even better have a wonderful year!

Andrew