Error assigning newly created file as alias

I have a script that works, but doesn’t always work. Sometimes it throws an error after it creates the new file and then tries to assign it to an alias. The following is the script:


tell application "Finder"
	
	set baseFile to choose file with prompt "Choose source file"
	set baseFileName to name of baseFile as text
	
	
	set storeChangeFolder to choose folder with prompt "Choose the location of your changes folder"
	
	--Get Folder for the Headers
	set headerFolder to choose folder with prompt "Choose the location of your headers"
	set myHeaderFiles to (every file of folder headerFolder whose name contains "indd")
	--set storeChangeFolder to storeChangeFolder as alias
	
	
	--Move file to store Change Folder
	set baseCopy to duplicate file baseFile to storeChangeFolder
	delay 5
	set baseCopy to baseCopy as alias
	--display dialog baseCopy as string
	--move baseCopy to storeChangeFolder
	
	--Repeat through the Folder
	repeat with aFile in myHeaderFiles
		
		--Get Store Name
		set currentFileName to name of aFile as text
		set currentFileName to characters 1 thru -6 of currentFileName as text
		
		--Creating a new file with the store name 
		set newFileName to my makeFileName(baseFileName, currentFileName)
		
		try
			set storeCopy to duplicate file baseCopy to storeChangeFolder
			delay 5
			set storeCopy to storeCopy as alias
		on error
			set currentFileName to name of aFile as text
			set currentFileName to characters 1 thru -6 of currentFileName as text
			
			--Creating a new file with the store name 
			set newFileName to my makeFileName(baseFileName, currentFileName)
			
			set storeCopy to duplicate file baseCopy to storeChangeFolder
			set storeCopy to storeCopy as alias
		end try
		
		
		tell application "Finder" to set the name of storeCopy to newFileName
		
		
		
		
		--Open Header File and Copy contents
		tell application "Adobe InDesign CS4"
			open aFile
			set myOpenHeaderFile to active document
			
			tell application "Finder" to open storeCopy
			set myOpenStoreCopy to active document
			
			--tell myOpenHeaderFile to select all page items
			set mySelection to every page item of page 1 of myOpenHeaderFile
			
			--get properties of mySelection 
			
			repeat with anItem in mySelection
				tell anItem
					set myHeader to duplicate to page 1 of myOpenStoreCopy
				end tell
			end repeat
			
			
			--Put store name in code
			
			tell active document
				
				set myStoreCode to page item "Code" of page 1
			end tell
			
			if (count of myStoreCode) = 1 then
				
				set find text preferences to nothing
				set change text preferences to nothing
				set find grep preferences to nothing
				set change grep preferences to nothing
				set find glyph preferences to nothing
				set change glyph preferences to nothing
				
				set find what of find text preferences to "Store Name"
				set change to of change text preferences to currentFileName
				
				
				set myFoundItems to find text
				
				
				tell parent story of myStoreCode
					tell paragraph 1
						change text
					end tell
				end tell
			end if
			
			tell active document
				set newPosition to page item "calendar position" of page 1
				set calendar to page item "effective dates" of page 1
			end tell
			
			set xPosition1 to (item 4 of geometric bounds of newPosition)
			set xPosition2 to (item 2 of geometric bounds of newPosition)
			set yPosition1 to (item 1 of geometric bounds of newPosition)
			set yPosition2 to (item 3 of geometric bounds of newPosition)
			
			--set xPosition to item 1 of newPosition
			--set yPosition to item 2 of newPosition
			move calendar to {xPosition2, yPosition1}
			
			close myOpenHeaderFile saving no
			close myOpenStoreCopy saving yes
			
			
		end tell
		
	end repeat
	
end tell




on makeFileName(myFileName, storeName)
	set oldDelims to AppleScript's text item delimiters -- save their current state
	try
		set AppleScript's text item delimiters to {"_"}
		set delimitedList to every text item of myFileName
		set AppleScript's text item delimiters to oldDelims -- restore them
		
		--build the new file name
		set itemDelimCount to count of text items of delimitedList
		if itemDelimCount = 4 then
			set firstPartName to text item 1 of delimitedList
			set secondPartName to text item 2 of delimitedList
			set thirdPartName to storeName
			set fourthPartName to text item 4 of delimitedList
			
			set newFileName to firstPartName & "_" & secondPartName & "_" & thirdPartName & "_" & fourthPartName
			
		else if itemDelimCount = 3 then
			set firstPartName to text item 1 of delimitedList
			set secondPartName to text item 2 of delimitedList
			set thirdPartName to storeName
			set fourthPartName to text item 3 of delimitedList
			
			set newFileName to firstPartName & "_" & secondPartName & "_" & thirdPartName & "_" & fourthPartName
		end if
		
		
		
	on error
		set AppleScript's text item delimiters to oldDelims -- restore them in case something went wrong
	end try
	
	return newFileName
end makeFileName



The error is: “error “Can’t make «class docf» "BVHBMT_0622_Base_p1.indd" of application "Finder" into type alias.” number -1700 from «class docf» “BVHBMT_0622_Base_p1.indd” to alias”

I have put the delays in hoping that would help, but it doesn’t. Also, I have the try statement on the second file, and that works, but it creates extra files that I don’t want/need. I am sure I have made this more complicated than it needs to be so any help would be greatly appreciated.

Why are you using alias when you don’t use it’s functionality. Use string paths instead and saves you a lot of trouble:)

duplicate file «class string» to folder «class string» will also do the job and don’t need alias at all.

I haven’t tried your solution, but how would you write that in the script? Do you just assign a variable to the path and say “as string” at the end? Thanks for your help?

I tried solutions I could come up with but nothing is working. My biggest issue is that I don’t understand exactly what is getting put into the variable when I am duplicating the baseCopy file. Is it a path to the duplicated file or is it just the file’s name? I can get around this issue on the first duplication because I know exactly what the filename will be once duplicated and what folder it is duplicated to. I then reconstruct the file path as a string and it works. I, unfortunately, can’t do that with the second duplication because I don’t know exactly what the file is being named. Thanks for your help

The result of Finder’s duplicate is a Finder reference, which can be used as-is (by Finder) or coerced to another form.

tell application "Finder" to (duplicate some file) -->finder reference
tell application "Finder" to (duplicate some file) as string -->a string path

Thanks for trying to clarify. If I am understanding you correctly, which is a big question right now, if I do:

tell application “Finder”
set storeCopy to duplicate file baseCopy to storeChangeFolder as string
end tell

That should return a string path to the newly duplicated file and I could then change the name by saying:

tell application “Finder”
set name of storeCopy to newFileName
end tell

I will try this tomorrow at work to verify that it works. Here is to hoping it does! Thanks again.

No. You have to change the name of the file reference, not the string.

tell application "Finder"
	set storeCopy to (duplicate file baseCopy to storeChangeFolder) -- not 'as string'
	set name of storeCopy to newFileName
end tell

Or, less efficiently because of the unnecessary coercion to string and subsequent reconstitution of the original reference:

tell application "Finder"
	set storeCopy to (duplicate file baseCopy to storeChangeFolder) as string
	set name of file storeCopy to newFileName -- NB. 'file'.
end tell

However, in both cases, the value of ‘storeCopy’ then becomes unusable, since it refers to the file under its original name which no longer exists. Your script goes on to open the renamed file, so you need to allow for this:

tell application "Finder"
	set name of (duplicate file baseCopy to storeChangeFolder) to newFileName
	set storeCopy to file newFileName of storeChangeFolder -- New Finder reference for the new name.
end tell

Hi,

I guess the main problem is in this line (and the other similar ones)

 set storeCopy to duplicate file baseCopy to storeChangeFolder as string

the variable baseCopy is defined as alias, so the additional keyword file causes a double referencing

The correct syntax is either (assumed within a Finder tell block)

 set storeCopy to duplicate file baseCopyStringPath to storeChangeFolder

or

 set storeCopy to duplicate baseCopyAlias to storeChangeFolder

To get the alias of the duplicated file you can easily write

 set storeCopy to (duplicate baseCopy to storeChangeFolder) as alias

You guys are geniuses! Thanks for that.

On another note, now I am getting an InDesign dialog telling me it can’t open one of the files (the file that has existed forever, not one of the newly duplicated files) because it is open by another user. I know the file isn’t open by anyone else and if I run the script again right away, it doesn’t stop on the same file. I didn’t have this problem until I introduced the suggestions by Nigel. Any ideas?

The following is the script as it stands now (the open aFile line inside the tell application “Adobe InDesign CS4” block is what is giving me the issue):


tell application "Finder"
	
	set baseFile to choose file with prompt "Choose source file"
	set baseFileName to name of baseFile as text
	
	
	set storeChangeFolder to choose folder with prompt "Choose the location of your changes folder"
	set storeChangeFolderPath to storeChangeFolder as string
	
	--Get Folder for the Headers
	set headerFolder to choose folder with prompt "Choose the location of your headers"
	set myHeaderFiles to (every file of folder headerFolder whose name contains "indd")
	--set storeChangeFolder to storeChangeFolder as alias
	
	
	--Move file to store Change Folder
	set baseCopy to duplicate file baseFile to storeChangeFolder
	set baseCopyPath to storeChangeFolderPath & baseFileName
	--display dialog baseCopyPath
	--display dialog baseCopy as string
	--move baseCopy to storeChangeFolder
	
	--Repeat through the Folder
	repeat with aFile in myHeaderFiles
		
		
		--Get Store Name
		set currentFileName to name of aFile as text
		set currentFileName to characters 1 thru -6 of currentFileName as text
		
		--Creating a new file with the store name 
		set newFileName to my makeFileName(baseFileName, currentFileName)
		
		set name of (duplicate file baseCopyPath to storeChangeFolder) to newFileName
		set storeCopy to file newFileName of storeChangeFolder
		
		
		
		--Open Header File and Copy contents
		tell application "Adobe InDesign CS4"
			
			open aFile
			set myOpenHeaderFile to active document
			
			open storeCopy
			set myOpenStoreCopy to active document
			
			--tell myOpenHeaderFile to select all page items
			set mySelection to every page item of myOpenHeaderFile
			
			--get properties of mySelection 
			
			repeat with anItem in mySelection
				tell anItem
					set myHeader to duplicate to page 1 of myOpenStoreCopy
					--move myHeader by {7.5, 0}
				end tell
			end repeat
			
			
			--Put store name in code
			
			tell active document
				
				set myStoreCode to page item "Code" of page 1
			end tell
			
			if (count of myStoreCode) = 1 then
				
				set find text preferences to nothing
				set change text preferences to nothing
				set find grep preferences to nothing
				set change grep preferences to nothing
				set find glyph preferences to nothing
				set change glyph preferences to nothing
				
				set find what of find text preferences to "Store Name"
				set change to of change text preferences to currentFileName
				
				
				set myFoundItems to find text
				
				
				tell parent story of myStoreCode
					tell paragraph 1
						change text
					end tell
				end tell
			end if
			
			tell active document
				set newPosition to page item "calendar position" of page 1
				set calendar to page item "effective dates" of page 1
			end tell
			
			set xPosition1 to (item 4 of geometric bounds of newPosition)
			set xPosition2 to (item 2 of geometric bounds of newPosition)
			set yPosition1 to (item 1 of geometric bounds of newPosition)
			set yPosition2 to (item 3 of geometric bounds of newPosition)
			
			--set xPosition to item 1 of newPosition
			--set yPosition to item 2 of newPosition
			move calendar to {xPosition2, yPosition1}
			
			
			close myOpenHeaderFile saving no
			close myOpenStoreCopy saving yes
			
			
		end tell
		
	end repeat
	
end tell




on makeFileName(myFileName, storeName)
	set oldDelims to AppleScript's text item delimiters -- save their current state
	try
		set AppleScript's text item delimiters to {"_"}
		set delimitedList to every text item of myFileName
		set AppleScript's text item delimiters to oldDelims -- restore them
		
		--build the new file name
		set itemDelimCount to count of text items of delimitedList
		if itemDelimCount = 4 then
			set firstPartName to text item 1 of delimitedList
			set secondPartName to text item 2 of delimitedList
			set thirdPartName to storeName
			set fourthPartName to text item 4 of delimitedList
			
			set newFileName to firstPartName & "_" & secondPartName & "_" & thirdPartName & "_" & fourthPartName
			
		else if itemDelimCount = 3 then
			set firstPartName to text item 1 of delimitedList
			set secondPartName to text item 2 of delimitedList
			set thirdPartName to storeName
			set fourthPartName to text item 3 of delimitedList
			
			set newFileName to firstPartName & "_" & secondPartName & "_" & thirdPartName & "_" & fourthPartName
		end if
		
		
		
	on error
		set AppleScript's text item delimiters to oldDelims -- restore them in case something went wrong
	end try
	
	return newFileName
end makeFileName



Thanks again!!

aFile is a Finder file specifier, which Indesign doesn’t recognize.
There are a few other specifier problems and you should avoid nested application tell blocks

I haven’t tested it but this should work


set baseFile to choose file with prompt "Choose source file"
set storeChangeFolder to choose folder with prompt "Choose the location of your changes folder"
set storeChangeFolderPath to storeChangeFolder as text
set headerFolder to choose folder with prompt "Choose the location of your headers"

tell application "Finder"
	
	set baseFileName to name of baseFile
	--Get Folder for the Headers
	
	set myHeaderFiles to (every file of headerFolder whose name contains "indd")
	
	--Move file to store Change Folder
	set baseCopy to duplicate baseFile to storeChangeFolder
	--Repeat through the Folder
	
end tell
set baseCopyPath to storeChangeFolderPath & baseFileName

repeat with aFile in myHeaderFiles
	
	tell application "Finder" to set currentFileName to name of aFile
	set currentFileName to text 1 thru -6 of currentFileName
	
	--Creating a new file with the store name 
	set newFileName to makeFileName(baseFileName, currentFileName)
	tell application "Finder"
		set name of (duplicate file baseCopyPath to storeChangeFolder) to newFileName
		set storeCopy to (file newFileName of storeChangeFolder) as alias
		
	end tell
	
	--Open Header File and Copy contents
	tell application "Adobe InDesign CS4"
		
		open (aFile as alias)
		set myOpenHeaderFile to active document
		
		open storeCopy
		set myOpenStoreCopy to active document
		
		--tell myOpenHeaderFile to select all page items
		set mySelection to every page item of myOpenHeaderFile
		
		--get properties of mySelection 
		
		repeat with anItem in mySelection
			tell anItem
				set myHeader to duplicate to page 1 of myOpenStoreCopy
				--move myHeader by {7.5, 0}
			end tell
		end repeat
		
		
		--Put store name in code
		
		tell active document
			
			set myStoreCode to page item "Code" of page 1
		end tell
		
		if (count of myStoreCode) = 1 then
			
			set find text preferences to nothing
			set change text preferences to nothing
			set find grep preferences to nothing
			set change grep preferences to nothing
			set find glyph preferences to nothing
			set change glyph preferences to nothing
			
			set find what of find text preferences to "Store Name"
			set change to of change text preferences to currentFileName
			
			
			set myFoundItems to find text
			
			
			tell parent story of myStoreCode
				tell paragraph 1
					change text
				end tell
			end tell
		end if
		
		tell active document
			set newPosition to page item "calendar position" of page 1
			set calendar to page item "effective dates" of page 1
		end tell
		
		set xPosition1 to (item 4 of geometric bounds of newPosition)
		set xPosition2 to (item 2 of geometric bounds of newPosition)
		set yPosition1 to (item 1 of geometric bounds of newPosition)
		set yPosition2 to (item 3 of geometric bounds of newPosition)
		
		--set xPosition to item 1 of newPosition
		--set yPosition to item 2 of newPosition
		move calendar to {xPosition2, yPosition1}
		
		
		close myOpenHeaderFile saving no
		close myOpenStoreCopy saving yes
		
		
	end tell
	
end repeat





on makeFileName(myFileName, storeName)
	set oldDelims to AppleScript's text item delimiters -- save their current state
	try
		set AppleScript's text item delimiters to {"_"}
		set delimitedList to every text item of myFileName
		set AppleScript's text item delimiters to oldDelims -- restore them
		
		--build the new file name
		set itemDelimCount to count of text items of delimitedList
		if itemDelimCount = 4 then
			set firstPartName to text item 1 of delimitedList
			set secondPartName to text item 2 of delimitedList
			set thirdPartName to storeName
			set fourthPartName to text item 4 of delimitedList
			
			set newFileName to firstPartName & "_" & secondPartName & "_" & thirdPartName & "_" & fourthPartName
			
		else if itemDelimCount = 3 then
			set firstPartName to text item 1 of delimitedList
			set secondPartName to text item 2 of delimitedList
			set thirdPartName to storeName
			set fourthPartName to text item 3 of delimitedList
			
			set newFileName to firstPartName & "_" & secondPartName & "_" & thirdPartName & "_" & fourthPartName
		end if
		
		
		
	on error
		set AppleScript's text item delimiters to oldDelims -- restore them in case something went wrong
	end try
	
	return newFileName
end makeFileName




That worked like a charm Stefan. Thanks and I can’t wait to dive in to the two sets of code to find where I went wrong. Thanks for the suggestions about nested application tell blocks. I am totally learning this on the fly and had no idea.