"Can't get item errors" on move after renaming files.

I’m reworking Folder Action scripts to use ‘on idle’ instead. I’m new to AS so I’m learning cool stuff. But I need help with the last part of the script that moves the renamed files. I’m just guessing by looking at other examples on how to set the files.

I’m missing something in the last couple of lines. Missing the step to redefine the renamed files so finder can move them. I don’t know enough about AppleScript to know what that exact step is. It compiles but I get "Can’t get item (item 1 of {class docf= filename here and so on through the folder path of the source folder.

on idle
	set PathToDesktop to path to the desktop as text
	set layersRenameFolder to (PathToDesktop & "On-Line Proofing1:1 Layout Proofs InDesign:Layout_Rename_LAYERS") as alias
	tell application "Finder" to set these_items to (every file of layersRenameFolder whose kind contains "PDF")
	
	--Check to see if there are one or more PDF files in the LayersRename folder on the desktop
	if the number of items in these_items is greater than 0 then
		
		tell me to activate
		display dialog "Files located. Script starting!" buttons " " default button 1 giving up after 2
		
		--Go through all PDF files, one at a time
		repeat with PDFfile in these_items
			--rename PDFFile
			set this_item to (item PDFfile of these_items)
			set fileName to name of PDFfile
			set AppleScript's text item delimiters to "-"
			if fileName ends with ("ALL.pdf" as text) then set newFileName to (text items 1 thru 2 of fileName) & "1.pdf" as text
			if fileName ends with ("CAR.pdf" as text) then set newFileName to (text items 1 thru 2 of fileName) & "1.pdf" as text
			if fileName ends with ("BOS.pdf" as text) then set newFileName to (text items 1 thru 2 of fileName) & "2.pdf" as text
			if fileName ends with ("BER.pdf" as text) then set newFileName to (text items 1 thru 2 of fileName) & "3.pdf" as text
			if fileName ends with ("HER.pdf" as text) then set newFileName to (text items 1 thru 2 of fileName) & "4.pdf" as text
			if fileName ends with ("YNK.pdf" as text) then set newFileName to (text items 1 thru 2 of fileName) & "5.pdf" as text
			if fileName ends with ("PAR.pdf" as text) then set newFileName to (text items 1 thru 2 of fileName) & "6.pdf" as text
			if fileName ends with ("BTN.pdf" as text) then set newFileName to (text items 1 thru 2 of fileName) & "7.pdf" as text
			if fileName ends with ("ELD.pdf" as text) then set newFileName to (text items 1 thru 2 of fileName) & "8.pdf" as text
			if fileName ends with ("CBPHYGE.pdf" as text) then set newFileName to (text items 1 thru 2 of fileName) & "9.pdf" as text
			if fileName ends with ("CBPHYD.pdf" as text) then set newFileName to (text items 1 thru 2 of fileName) & "9.pdf" as text
			if fileName ends with ("CBPHY.pdf" as text) then set newFileName to (text items 1 thru 2 of fileName) & "9.pdf" as text
			
			
			set name of new_item to newFileName
			
			tell application "Finder" to move new_item to folder "Out" of folder "1 Layout Proofs InDesign" of folder "On-Line Proofing1" of folder "Desktop" of folder "saolpimage1" of folder "Users" of startup disk with replacing
			
		end repeat
	end if
	return 5
end idle

set “dfolder” in the below script to the location of your “out” folder. The script will work on your pdf files having names all.pdf and bos.pdf.

set ifolder to path to desktop folder from user domain
set dfolder to "Leopard:Users:chris:Desktop:out:"
set qdfolder to quoted form of (POSIX path of dfolder)
set moveList to {{thename:{"all.pdf"}, therename:"1.pdf"}, {thename:"bos.pdf", therename:"2.pdf"}}
tell application "Finder"
	set allfiles to every file in folder ifolder whose name extension is "pdf"
	repeat with afile in allfiles
		set afilename to name of afile
		set afiletext to afile as text
		repeat with moveItem in moveList
			
			if afilename is in (thename of moveItem) then
				set newname to (therename of moveItem)
				
				set name of file afilename to newname
				set thenewfilepath to ifolder & newname as text
				set thepath to quoted form of (POSIX path of thenewfilepath)
				set thecmd to "mv -n " & thepath & " " & qdfolder
				do shell script thecmd
			end if
			end repeat
	end repeat
end tell

Hi,

replacing Folder Actions with stay open scripts is not recommended, because it wastes resources by polling the folders unnecessarily 90% percent of the time.

Stefan, thanks for advice.

But the scripts run on what is a script server – a dedicated machine that processes up to one hundred files a day with these scripts. I’ve migrated these FAs from 10.4 G4 hardware to 10.5 MacPros and the FAs don’t trigger consistently. From searching these forums I’ve seen the recommendation to try on idle and so I want to test it. I’ve started testing and I like the results I’m seeing but now I’m stuck on the transition from renaming the files via text delimiters and moving them to the results folder. (In my searching I’ve seen your comment before to not use on idle. ('-: )

Regardless, this is a learning opportunity for me to grow in knowledge of AppleScript.

I still want to know how to resolve the error I’m receiving. And from what I’ve seen on the boards I highly respect your opinion and even more your knowledge of applying AppleScript. (I’ve been awaiting your response – really.) Your generosity in sharing your knowledge is gratefully appreciated.

Best regards,

brad

Chrisd, thanks for the response.

I’ll take a look at it tomorrow. Leaving now for U2 concert in Chicago tonight.

In my script I only included 10 of the rename instances. There are nearly 40 total instances I need to account for.

The main problem is this line


set name of new_item to newFileName

AppleScript itself cannot set a name of a file/folder without Finder or System Events
and the variable new_item is not defined before.

If you don’t trust Folder Actions you should use launchd agents to watch the folders, which can trigger AppleScripts

PS:

this cannot work either:


repeat with PDFfile in these_items
           --rename PDFFile
           set this_item to (item PDFfile of these_items)

These forms are correct:

repeat with PDFfile from 1 to (count these_items)
	--rename PDFFile
	set this_item to (item PDFfile of these_items)
end repeat
repeat with PDFfile in these_items
	--rename PDFFile
	set this_item to contents of PDFfile
end repeat

If you do not want to use Folder Action scripts then checkout Stefan’s article on using launchd.

I’m not getting it.

I used this form.

The filename changes to the newFileName but still won’t move. The error indicates Finder is trying to move the original file and can’t find it.

on idle
	set PathToDesktop to path to the desktop as text
	set layersRenameFolder to (PathToDesktop & "On-Line Proofing1:1 Layout Proofs InDesign:Layout_Rename_LAYERS") as alias
	tell application "Finder" to set these_items to (every file of layersRenameFolder whose kind contains "PDF")
	
	--Check to see if there are one or more PDF files in the LayersRename folder on the desktop
	if the number of items in these_items is greater than 0 then
		
		tell me to activate
		display dialog "Files located. Script starting!" buttons " " default button 1 giving up after 2
		
		--Go through all PDF files, one at a time
		repeat with PDFfile in these_items
			--rename PDFFile
			set this_item to contents of PDFfile
			set fileName to name of this_item
			set AppleScript's text item delimiters to "-"
			if fileName ends with ("ALL.pdf" as text) then set newFileName to (text items 1 thru 2 of fileName) & "1.pdf" as text
			if fileName ends with ("CAR.pdf" as text) then set newFileName to (text items 1 thru 2 of fileName) & "1.pdf" as text

			
			set name of this_item to newFileName
			(*set this_item to (contents of PDFfile)set this_item to newFileName*)
			
			tell application "Finder" to move this_item to folder "Out" of folder "1 Layout Proofs InDesign" of folder "On-Line Proofing1" of folder "Desktop" of folder "saolpimage1" of folder "Users" of startup disk with replacing
			
		end repeat
	end if
	return 5
end idle

property layersRenameFolder : missing value
property outFolder : missing value

on idle
	if layersRenameFolder is missing value then
		set PathToDesktop to path to desktop as text
		set layersRenameFolder to (PathToDesktop & "On-Line Proofing1:1 Layout Proofs InDesign:Layout_Rename_LAYERS") as alias
		set outFolder to (PathToDesktop & "On-Line Proofing1:1 Layout Proofs InDesign:Out") as alias
	end if
	tell application "Finder" to set these_items to (every file of layersRenameFolder whose kind contains "PDF")
	
	--Check to see if there are one or more PDF files in the LayersRename folder on the desktop
	if (count these_items) is greater than 0 then
		
		tell me to activate
		display dialog "Files located. Script starting!" buttons " " default button 1 giving up after 2
		
		--Go through all PDF files, one at a time
		repeat with PDFfile in these_items
			--rename PDFFile
			set this_item to contents of PDFfile
			set fileName to name of this_item
			set {TID, text item delimiters} to {text item delimiters, "-"}
			if fileName ends with "ALL.pdf" or fileName ends with "CAR.pdf" then set newFileName to (text items 1 thru 2 of fileName) & "1.pdf" as text
			set text item delimiters to TID
			tell application "Finder"
				set name of this_item to newFileName
				move this_item to outFolder with replacing
			end tell
		end repeat
	end if
	return 5
end idle

I see in your script you use ‘property’ to define layersRenameFolder and outFolder with an initial value.

The first ‘if’ statement then resolves as true and thus assigns the canonical paths to the variables (my intended ‘in’ and ‘out’ folders).

However, running the script results in same error – after the dialog displays and the file is renamed, the finder can’t move/get the file because it wants to move/get the original PDF; the name of the file before the rename.

Red_Menace over at AppleScript discussions at Apple - Support - Discussions provided a solution that worked for me and penetrated my obtuse brain – getting the container and the result was the difference:

My gratitude to Stefan and others who took the time to work with me.

If you coerce the file specifier to an alias, it works also


.
set this_item to contents of PDFfile as alias
set fileName to name of (info for this_item)
.
move this_item to outFolder with replacing
.

Note: if a file doesn’t match the condition ALL.pdf or CAR.pdf the script aborts because the variable newFileName won’t be defined

Understood; regarding the potential for unmatched condition.

There is an export script that sets the endings of these filenames to the required endings. But there is always the chance for error.

There are 70 different defined file endings when considering the combinations. (I only included a few in these discussions)

It’d be nice to have a routine that moves any unmatched file to another folder without stopping the flow of matched files. Then when it is noticed the expected results files are missing we have a location to check for bad files and then correct them and reprocess them.

But that is beyond my ability.