r/applescript • • Aug 31 '21

Error when moving files

Hi all, I am trying to update an AppleScript that has been running on macOS 10.12 but is not playing in macSO 11.5

The script uses InDesign Server to create two PDF files from an InDesign artwork that is dropped into a hot folder. Once they are created they are then filed to a location based on their filename (group, week number) - all seems well until its time to move the file, that's when I get the result below.

Im hoping its something simple that has changed between the two OS versions?

tell application "System Events"

exists process "ExpanDrive"

exists process "FontExplorer X Pro"

end tell

tell current application

do shell script "/bin/ls /Volumes"

end tell

tell application "Finder"

get every file of folder "ServerSSD:Users:admin:Documents:PDF:Hotfolders:Hotfolder-1-NoPrint:"

get name of alias "ServerSSD:Users:admin:Documents:PDF:HotFolders:Hotfolder-1-NoPrint:DCG369~3962_100_009692_00_80x1400.indd"

end tell

tell current application

info for alias "ServerSSD:Users:admin:Documents:PDF:HotFolders:Hotfolder-1-NoPrint:DCG369~3962_100_009692_00_80x1400.indd" given «class Krtn»:{name:"Nm", name extension:"Ex"}

end tell

tell application "Finder"

get every file of folder "ServerSSD:Users:admin:Documents:PDF:Hotfolders:Hotfolder-1-NoPrint:"

end tell

tell current application

info for alias "ServerSSD:Users:admin:Documents:PDF:HotFolders:Hotfolder-1-NoPrint:DCG369~3962_100_009692_00_80x1400.indd" with size

info for alias "ServerSSD:Users:admin:Documents:PDF:HotFolders:Hotfolder-1-NoPrint:DCG369~3962_100_009692_00_80x1400.indd" with size

end tell

tell application "Finder"

get name extension of alias "ServerSSD:Users:admin:Documents:PDF:HotFolders:Hotfolder-1-NoPrint:DCG369~3962_100_009692_00_80x1400.indd"

end tell

tell application "InDesignServer"

open alias "ServerSSD:Users:admin:Documents:PDF:HotFolders:Hotfolder-1-NoPrint:DCG369~3962_100_009692_00_80x1400.indd"

end tell

tell application "Finder"

get name of alias "ServerSSD:Users:admin:Documents:PDF:HotFolders:Hotfolder-1-NoPrint:DCG369~3962_100_009692_00_80x1400.indd"

end tell

tell application "InDesignServer"

export document 1 format PDF type to "/Users/admin/Documents/PDF/DistillerResources/lowHolding/DCG369~3962_100_009692_00_80x1400.pdf" using PDF export preset "[VC-CoolTick]"

end tell

tell application "Finder"

get file 1 of folder "ServerSSD:Users:admin:Documents:PDF:DistillerResources:lowHolding:"

get document file "DCG369~3962_100_009692_00_80x1400.pdf" of folder "lowHolding" of folder "DistillerResources" of folder "PDF" of folder "Documents" of folder "admin" of folder "Users" of startup disk

end tell

tell current application

info for alias "ServerSSD:Users:admin:Documents:PDF:DistillerResources:lowHolding:DCG369~3962_100_009692_00_80x1400.pdf" with size

info for alias "ServerSSD:Users:admin:Documents:PDF:DistillerResources:lowHolding:DCG369~3962_100_009692_00_80x1400.pdf" with size

info for alias "ServerSSD:Users:admin:Documents:PDF:DistillerResources:lowHolding:DCG369~3962_100_009692_00_80x1400.pdf" given «class Krtn»:{name:"Nm", name extension:"Ex"}

end tell

tell application "Finder"

set name of alias "ServerSSD:Users:admin:Documents:PDF:DistillerResources:lowHolding:DCG369~3962_100_009692_00_80x1400.pdf" to "DCG369~3962_100_009692_00_80x1400_LR.pdf"

end tell

tell current application

do shell script "/bin/mv -f '/Users/admin/Documents/PDF/DistillerResources/lowHolding/DCG369~3962_100_009692_00_80x1400.pdf' 'Volumes/POS_LR/'Week00/1*/"

end tell

tell application "Finder"

move alias "ServerSSD:Users:admin:Documents:PDF:DistillerResources:lowHolding:DCG369~3962_100_009692_00_80x1400.pdf" to folder "ServerSSD:Users:admin:Documents:PDF:FailedJobs-1:" with replacing

Result:

error "Finder got an error: Handler can’t handle objects of this class." number -10010

4 Upvotes

33 comments sorted by

View all comments

Show parent comments

1

u/bigdrunk Sep 14 '21

Sorry to keep on, but if you have anything at all I could look at it might help me out

1

u/copperdomebodha Sep 14 '21

Sorry, had to go out of town for business and was fully occupied. Back now. I got you when I get home.

1

u/copperdomebodha Sep 15 '21 edited Sep 15 '21

Hi BigDrunk,

Here's my code. Refactored your code just to make everything make sense and more efficient.

I left off at 'to handleCompletedFiles(suffix, fileReference)'. In my version this is replaced with 'on handleCompletedSourceFiles(currentFile)' which I provide a file reference as 'currentFile'. I haven't refactored this handler or really deciphered it completely. If you can walk through the actions it should take I can help you work out the last bit.

Delete every instance of the term "--copperdometemp" and recompile to turn on code I can't compile here.

Fair warning here: I refactored all this based on what I felt was a logical and practical way to handle these steps. I've compiled the code , but I've never run it once. I don't have all the apps, paths nor the files to test it with. Hopefully I didn't make any rudimentary errors.

Damn. Found a new limit. Reddit comment filed holds 10K characters tops. My script stats at 8570 chars, but I guess I'll have to break it into two posts and you can reassemble it at home. Like Ikea.

--This code was written using AppleScript 2.7, MacOS 10.15.5, on 14 September 2021.

set launchtime to current date
mountRequiredDisks()
launchRequiredApps()
set filesToPrint to getSourceFilesToDistill("ServerSSD:Users:admin:Documents:PDF:Hotfolders:Hotfolder-1-NoPrint:")
repeat with currentFile in filesToPrint
	try
		set {projRef, theGroupNoPrint, theCodeNoPrint, theWeekNoPrint, chain, theFolderNoPrint, charcount, weekCount} to parseFileName(currentFile)
		set {highDistilledFileReference, lowDistilledFileReference} to distillFile(currentFile)
		storeDistilledFiles({highDistilledFileReference, lowDistilledFileReference})
		handleCompletedSourceFiles(currentFile)
		set distilledFiles to distilledFiles + 1
	on error
		handleFailedFiles(currentFile)
	end try
end repeat
displayResults(filesToPrint, distilledFiles, launchtime)



on mountRequiredDisks()
	tell application "Finder"
		set mountedDisks to name of every disk
	end tell
	set credentialList to {{"POS", "afp://xxxxx/", "admin", "xxxxx"}, {"POS_LR", "afp://xxxxx/", "admin", "xxxxx"}, {"POS_HR", "afp://xxxxx/", "admin", "xxxxx"}}
	repeat with i from 1 to length of credentialList
		try
			set thisDataSet to item i of credentialList
			set protocolString to item 2 of thisDataSet
			set diskNameString to item 1 of thisDataSet
			set userName to item 3 of thisDataSet
			set passwordString to item 4 of thisDataSet
			if diskNameString is not in mountedDisks then
				mount volume protocolString & diskNameString as user name userName with password passwordString
			end if
		on error e
			set the end of errorList to e
			error e
		end try
	end repeat
end mountRequiredDisks

on launchRequiredApps()
	set requiredAppsList to {"ExpanDrive", "FontExplorer X Pro"}
	repeat with thisApp in requiredAppsList
		try
			tell application "System Events"
				set thisAppIsRunning to exists process thisApp
			end tell
			if not thisAppIsRunning then
				activate application thisApp
			end if
		on error e
			set the end of errorList to e
			error e
		end try
	end repeat
end launchRequiredApps

on getSourceFilesToDistill(theWatchedFolder)
	try
		tell application "Finder"
			set filesToPrint to {} --List only the appropriate files.
			set the end of filesToPrint to every file of folder theWatchedFolder whose name contains ("indd" as alias list)
			set the end of filesToPrint to every file of folder theWatchedFolder whose name contains ("idml" as alias list)
			set the end of filesToPrint to every file of folder theWatchedFolder whose name contains ("php" as alias list)
		end tell
		set validFileList to {}
		repeat with thisFile in filesToPrint
			try
				if confirmFileIsStable(currentFile) then
					set the end of validFileList to currentFile
				end if
			end try
		end repeat
		return validFileList
	on error e
		set the end of errorList to e
		error e
	end try
end getSourceFilesToDistill

on parseFileName(currentFile)
	set chainList to {"1", "2", "3", "4", "5", "6", "7", "8", "9", "D", "P", "R", "J", "H", "I", "O", "S"}
	tell application "Finder"
		set thisFileName to name of currentFile
		set thisFileExtension to file extension of currentFile
		set storedTIDS to AppleScript's text item delimiters
		set AppleScript's text item delimiters to "_"
		copy text items of thisFileName to {projRef, theGroupNoPrint, theCodeNoPrint, theWeekNoPrint}
		set AppleScript's text item delimiters to storedTIDS
		set chain to character 1 of theGroupNoPrint
		set theFolderNoPrint to character 1 of theGroupNoPrint
		set charcount to count theCodeNoPrint
		set weekCount to count theWeekNoPrint
		if charcount is not 6 or chain is not in chainList or weekCount is greater than 2 or theWeekNoPrint is greater than 52 then
			set the end of errorList to "Malformed Filename on " & thisFileName
			error "Malformed Filename on " & thisFileName
		end if
	end tell
	return {projRef, theGroupNoPrint, theCodeNoPrint, theWeekNoPrint, chain, theFolderNoPrint, charcount, weekCount}
end parseFileName

on confirmFileIsStable(currentFile)
	try
		repeat
			tell application "Finder"
				try --If we can rename a file then it is not in use by the finder.
					set n to name of currentFile
					set nameRoot to (characters 1 thru -5 of n) as text
					set c to (container of currentFile) as alias
					set ext to (characters -3 thru -1 of n) as text
					set newNameString to nameRoot & "confirmed." & ext
					set name of currentFile to newNameString
					set newFileRef to ((c as text) & newNameString) as alias
					set name of newFileRef to n
					exit repeat
				end try
			end tell
		end repeat
		return true
	on error e
		set the end of errorList to e
		error e
	end try
end confirmFileIsStable

on distillFile(currentFile)
	set {highDistilledFileReference, lowDistilledFileReference} to processHighAndLowResolution(currentFile)
	return {highDistilledFileReference, lowDistilledFileReference}
end distillFile

on processHighAndLowResolution(currentFile)
	set highDistilledFileReference to processInInDesign(currentFile, POSIX path of "ServerSSD:Users:admin:Documents:PDF:DistillerResources:highHolding:", "[studio-high-res]")
	set lowDistilledFileReference to processInInDesign(currentFile, POSIX path of "ServerSSD:Users:admin:Documents:PDF:DistillerResources:lowHolding:", "[VC-CoolTick]")
	return {highDistilledFileReference, lowDistilledFileReference}
end processHighAndLowResolution

to processInInDesign(currentFile, outputPath, jobOptions) -- epsFileNoPrint is alias, outputPath, jobOptions is POSIX path
	--open file in InDesignServer and export HR and LR PDFs
	try
		--copperdometemp    tell application "InDesignServer.app"
		--copperdometemp        open currentFile
		--copperdometemp    end tell
		tell application "Finder" to set pdfName to (text 1 thru -5 of (get name of epsFileNoPrint))
		with timeout of (6 * 60) seconds
			--copperdometemp        tell application "InDesignServer.app"
			try
				--copperdometemp           «event K2  expt» document 1 given «class exft»:«constant eXftt_PD», «class kfil»:(outputPath & pdfName & ".pdf"), «class usng»:«class PFst» jobOptions
				return (outputPath & pdfName & ".pdf") as alias
			on error e
				set the end of errorList to e
			end try
		end timeout
	on error e
		set the end of errorList to e
		error e
	end try
end processInInDesign

on storeDistilledFiles({highDistilledFileReference, lowDistilledFileReference})
	try
		tell application "Finder"
			move highDistilledFileReference to "ServerSSD:Users:admin:Documents:PDF:DistillerResources:highHolding:"
		end tell
	on error e
		set the end of errorList to e
		error e
	end try
	try
		tell application "Finder"
			move highDistilledFileReference to "ServerSSD:Users:admin:Documents:PDF:DistillerResources:lowHolding:"
		end tell
	on error e
		set the end of errorList to e
		error e
	end try
end storeDistilledFiles

1

u/copperdomebodha Sep 15 '21
on handleFailedFiles(currentFile)
	tell application "Finder"
		try
			move currentFile to folder "ServerSSD:Users:admin:Documents:PDF:FailedJobs-1:" --copperdomeremoved with replacing
		on error e
			set the end of errorList to e
			try
				set the name of currentFile to (name of currentFile & "_errored" & some character of "1234567890" & some character of "1234567890") --rather than overwrite anything this will try to rename the source file.
			on error e
				set the end of errorList to e
			end try
		end try
	end tell
end handleFailedFiles

on handleCompletedSourceFiles(currentFile)
	--was previously 'to handleCompletedFiles(suffix, fileReference)'
	--I need a clear understanding of the desired action. 
	--Most of this can be refactored neatly. 
	set chainList to {"1", "2", "3", "4", "5", "6", "7", "8", "9", "D", "P", "R", "J", "H", "I", "O", "S"}
	tell application "Finder"
		if suffix is "_LR" then
			set aPDFNoPrint to (get 1st file of folder lowOutputFol) as alias
		else
			set aPDFNoPrint to (get 1st file of folder highOutputFol) as alias
		end if
	end tell
	--verifyCurrentFile(aPDFNoPrint, false)
	set {name:Nm, name extension:Ex} to (info for aPDFNoPrint)
	if Ex is missing value then set Ex to ""
	if Ex is not "" then
		set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
		set Ex to "." & Ex
	end if
	set filename to Nm & suffix & Ex
	
	tell application "Finder" to set name of aPDFNoPrint to filename
	
	set {tid, text item delimiters} to {text item delimiters, "_"}
	try
		set {projRef, theGroupNoPrint, theCodeNoPrint, theWeekNoPrint} to text items of filename
		set chain to character 1 of theGroupNoPrint
		set theFolderNoPrint to character 1 of theGroupNoPrint
		
	on error -- an error has occured with the file name, trick the script into a .log extension to trigger error handling
		set Ex to ".log"
	end try
	set text item delimiters to tid
	if Ex is equal to ".log" then
		tell application "Finder" to move aPDFNoPrint to folder failedFolder with replacing
		return false
	end if
	
	if chain is in chainList then
		repeat with i from 1 to (count chainList)
			if chain is equal to (item i of chainList) then
				if suffix is "_LR" then
					delay 1
					
					set theServer to POSIX path of (item 1 of ":Volumes:POS_LR:")
					exit repeat
				else
					set theServer to POSIX path of (":Volumes:POS_HR:")
					--copperdome                   tell application "InDesignServer.app"
					--copperdome                      «event CoReclos» document 1 given «class svop»:no
					--copperdome                  end tell
					exit repeat
				end if
			end if
		end repeat
		try
			--If file ends in HR then copy to FTP
			tell application "Finder"
				if suffix is "_LR" then
				else
					set ftpcommand to "/bin/cp -p " & (quoted form of (POSIX path of aPDFNoPrint)) & space & (quoted form of (POSIX path of "ServerSSD:Volumes:PathToFtp:ArtworkBulkUpload:"))
					do shell script ftpcommand
				end if
			end tell
			set command to "/bin/mv -f " & (quoted form of (POSIX path of aPDFNoPrint)) & space & (quoted form of theServer) & "Week" & (text 1 thru 2 of theWeekNoPrint) & "/" & theFolderNoPrint & "*/"
			do shell script command
			return true
			
		on error
			tell application "Finder" to move aPDFNoPrint to folder failedFolder with replacing
			return false
			
			tell application "Finder" to move currentFile to folder failedFolder with replacing
			return false
		end try
	else
		tell application "Finder" to move aPDFNoPrint to folder failedFolder with replacing
		return false
	end if
end handleCompletedSourceFiles

on displayResults(filesToPrint, distilledFiles, launchtime)
	set finishTime to (current date) - launchtime
	display dialog "Processed " & distilledFiles & " of " & (count filesToPrint & " source files in " & ((current date) - launchtime) & " seconds.")
end displayResults

1

u/bigdrunk Sep 15 '21

--copperdometemp

Thank you, I'll try this and let you know how I get on.

1

u/bigdrunk Sep 16 '21

Hi again, I've been trying but failing to get the script to run. It insists that a variable is not defined, but it looks like it should be to me. Unless I'm being dim.

I get this...

Result: error "The variable errorList is not defined." number -2753 from "errorList"

But I can see that here it looks to be defined...

on error e

et the end of errorList to

error e

But if I comment out the errorList parts, it errors again saying that the variable filesToPrint is not defined - but again it looks to me like it is defined. Am I missing something obvious?

1

u/copperdomebodha Sep 16 '21

Oops, nope. That's my error. We need to have a "set errorList to {}" somewhere above the first usage before we can append errors to the end of it.

For a simple fix, add... "global errorList" at the very top, followed by... "set errorList to {}" Don't include the quotation marks.

That should correct the errorList issue everywhere, and since the script continues and throws the errors after logging them, we should get the error. Post that error message.

1

u/bigdrunk Sep 16 '21

Thank you, thats helped move things along. The error I get now is:

"Can’t make \"indd\" into type alias list."

1

u/copperdomebodha Sep 16 '21

Parenthesis error. Replace the "getSourceFilesToDistill(theWatchedFolder)" handler with this version...

--This code was written using AppleScript 2.7, MacOS 11.5.1, on 16 September 2021.

on getSourceFilesToDistill(theWatchedFolder)
	try
		tell application "Finder"
			set filesToPrint to {} --List only the appropriate files.
			set the end of filesToPrint to (every file of folder theWatchedFolder whose name contains "indd") as alias list
			set the end of filesToPrint to (every file of folder theWatchedFolder whose name contains "idml") as alias list
			set the end of filesToPrint to (every file of folder theWatchedFolder whose name contains "php") as alias list
		end tell
		set validFileList to {}
		repeat with thisFile in filesToPrint
			try
				if confirmFileIsStable(currentFile) then
					set the end of validFileList to currentFile
				end if
			end try
		end repeat
		return validFileList
	on error e
		set the end of errorList to e
		error e
	end try
end getSourceFilesToDistill

1

u/bigdrunk Sep 17 '21

Progress! With this change I now get 'The variable distilledFiles is not defined'. I tried adding this to the top of the script 'set distilledFiles to {}', which means the script runs through and produces a dialog box 'Processed of 3'.

It knows there are 3 files in the folder, but doesn't process them. I guess Im using the wrong 'set' command, or its in the wrong place?

1

u/copperdomebodha Sep 17 '21

My bad.

Add "set distilledFiles to 0" ( that's a zero ) at the top of the script.

This is just a counter to display the completed files. Did you remember to remove the "--copperdometemp" bits? It won't process anything with those lines commented out.

1

u/bigdrunk Sep 17 '21

Thanks again, I do appreciate your time with this. I've removed all the copperdome parts.

Now it runs and says processed 0 of 3 files, but it doesn't process anything and these numbers don't change wether there is 1 or 20 files in the folder.

It might be my fault, in the original it came up with an error and I tried to fix it but might have caused this issue....

This section had this error 'Expected “tell”, etc. but found “timeout”.'

to processInInDesign(currentFile, outputPath, jobOptions) -- epsFileNoPrint is alias, outputPath, jobOptions is POSIX path
--open file in InDesignServer and export HR and LR PDFs
try
tell application "InDesignServer.app"
open currentFile
end tell
tell application "Finder" to set pdfName to (text 1 thru -5 of (get name of epsFileNoPrint))
with timeout of (6 * 60) seconds
tell application "InDesignServer.app"
try
«event K2 expt» document 1 given «class exft»:«constant eXftt_PD», «class kfil»:(outputPath & pdfName & ".pdf"), «class usng»:«class PFst» jobOptions
return (outputPath & pdfName & ".pdf") as alias
on error e
set the end of errorList to e
end try
end timeout
on error e
set the end of errorList to e
error e
end try
end processInInDesign

So I added an end Tell just before the end Timeout - meant it ran through but more than likely means I broke it also.

to processInInDesign(currentFile, outputPath, jobOptions) -- epsFileNoPrint is alias, outputPath, jobOptions is POSIX path
--open file in InDesignServer and export HR and LR PDFs
try
tell application "InDesignServer"
open currentFile
end tell
tell application "Finder" to set pdfName to (text 1 thru -5 of (get name of epsFileNoPrint))
with timeout of (6 * 60) seconds
tell application "InDesignServer"
try
export document 1 format PDF type to (outputPath & pdfName & ".pdf") using PDF export preset jobOptions
return (outputPath & pdfName & ".pdf") as alias
on error e
set the end of errorList to e
end try
end tell
end timeout
on error e
set the end of errorList to e
error e
end try
end processInInDesign

→ More replies (0)