Rename & Re-linking

Hi,
I a indesign document with 4 images of name logo.eps (link from “Mac:Folder 1:”), logo.eps (link from “Mac:Folder 2:”), logo.eps (link from “Mac:Folder 3:”), logo.eps (link from “Mac:Folder 4:”). I want to copy all these image file to a location “Mac:Images:”. Since the name is the same so it will overwrite all the files and only on image will get copy.

What I am looking is it should popup a display like


display dialog "Please select one option" buttons {"Rename", "Overwrite", "Ignore"}
If button returned to result is "Overwrite" then
duplicate file..... folder .... etc


For Rename it should popup dialog box

display dialog "Please enter name" button {"OK"} default answer ""

For Ignore it just do nothing.
I need help on renaming and ignoring. The above code is not compiled.

Regards,
Poo

Hi,

take a look at this thread.

Hi Stefan,
I have gone through this code but somewhere is doesn’t solve my purpose, some where either I am doing wrong or I got confused.
My mac laptop is down to to some problem, so I am unable to send my complete code. I will update you with my code so that you can understand what I am looking for.

Regards,
Poo

Dear All,
I have an Indesign document with 4 images placed on it say testimage.jpg one from desktop, second from document folder, third from “mac/test/article” and third from location of indesign document.

What I am looking for if any images linked from any location other than indesign document location then it should duplicate those images to the location of indesign document, but since testimage1.jpg is already available so either we have to re-name it or, overwrite or ignore.

In case of re-name testimage.jpg should get renamed with the text entered by the user to the same location and then link this file form that location, after linking the re-named image should get duplicated to the location of indesign document and again re-link it.

In the case of Overwrite if user clicks on “Overwrite” simple it overwrite existing file to the location of InDesign document and re-linked with the new file.

In case of Ignore, file should not get copied to the location of InDesign document and re-link with the older file only.

There is one more instance involved say if one testimage.jpg from desktop copied and pasted 20 times in indesign document and since the path is the same for all these 20 images so first it should ask for re-name and then re-link all these 20 images from the same place.

I am trying below code and facing problem while doing re-name.


set myFolderPath to "Macintosh:Poo_Test:Cam_475_IV:"
tell application "Adobe InDesign CS4"
	set myDoc to document 1
	tell myDoc
		repeat with oneLink in (get links)
			set linkPath to file path of oneLink
			display dialog linkPath
			if linkPath is not (myFolderPath & name of oneLink) then
				--tell application "Finder" to return container of linkPath as text
				--get linkFolderPath to (get path of linkPath)
				--				display dialog linkFolderPath
				--------New Code for force replacement------------
				tell application "Finder"
					try
						set movedFile to (duplicate file linkPath to folder myFolderPath)
					on error
						display dialog "File already exist with that name" & return & return & "Please click on an action!" buttons {"Rename", "Overwrite", "Ignore"}
						if button returned of result is "Overwrite" then
							set movedFile to (duplicate file linkPath to folder myFolderPath with replacing) as alias
						else if button returned of result is "Rename" then
							display dialog "Enter Name:" buttons {"OK"} default button 1 default answer ""
							set renameFile to text returned of result
							set renameFile to linkPath & renameFile
							set file linkPath to renameFile
							set movedFile to (duplicate file linkPath to folder myFolderPath with replacing) as alias
						else if button returned of result is "Ignore" then
							
						end if
					end try
				end tell
				-------------End of code----------------
				--								tell application "Finder" to set movedFile to (duplicate file linkPath to myFolderPath) as alias
				relink (contents of oneLink) to movedFile
				update oneLink
			end if
		end repeat
		
	end tell
end tell

Regards,
Poo

:frowning:
Rename is still an issue. I am trying as well, please help me out.

Regards,
Poo

Below updated code is for Rename, but still it is showing errors:

set myFolderPath to "Macintosh:Poo_Test:Cam_475_IV:"
tell application "Adobe InDesign CS4"
	set myDoc to document 1
	tell myDoc
		repeat with oneLink in (get links)
			set linkPath to file path of oneLink
			display dialog linkPath
			if linkPath is not (myFolderPath & name of oneLink) then
				tell application "Finder" to set linkFolderPath to (container of (linkPath as alias)) as text
				--get linkFolderPath to (get path of linkPath)
				display dialog linkFolderPath
				--------New Code for force replacement------------
				tell application "Finder"
					try
						set movedFile to (duplicate file linkPath to folder myFolderPath)
					on error
						display dialog "File already exist with that name" & return & return & "Please click on an action!" buttons {"Rename", "Overwrite", "Ignore"}
						if button returned of result is "Overwrite" then
							set movedFile to (duplicate file linkPath to folder myFolderPath with replacing) as alias
						else if button returned of result is "Rename" then
							display dialog "Enter Name:" buttons {"OK"} default button 1 default answer ""
							set renameFile to text returned of result
							set renameFile to linkFolderPath & renameFile
							set file linkPath to renameFile
							set movedFile to (duplicate file linkPath to folder myFolderPath with replacing) as alias
						else if button returned of result is "Ignore" then
							
						end if
					end try
				end tell
				-------------End of code----------------
				--								tell application "Finder" to set movedFile to (duplicate file linkPath to myFolderPath) as alias
				relink (contents of oneLink) to movedFile
				update oneLink
			end if
		end repeat
		
	end tell
end tell

Regards,
Poo

I am getting the below error

Regards,
Poo

try this, it uses a shell call to rename and duplicate the file at the same time,
the original file will not be touched. The handler is to avoid nested tell blocks


property myFolderPath : "Macintosh:Poo_Test:Cam_475_IV:"

tell application "Adobe InDesign CS3"
	set myDoc to document 1
	tell myDoc
		repeat with oneLink in (get links)
			set linkPath to file path of oneLink
			if linkPath is not (myFolderPath & name of oneLink) then
				set movedFile to my processLink(linkPath)
				if movedFile is not missing value then
					relink (contents of oneLink) to movedFile
					update oneLink
				end if
			end if
		end repeat	
	end tell
end tell

on processLink(link)
	tell application "Finder"
		tell file link
			set linkFileName to its name
			set linkFolderPath to its container as text
			set linkFileExtension to name extension
		end tell
		
		if exists file (myFolderPath & linkFileName) then
			set {button returned:buttonReturned} to display dialog ¬
				"File already exist with that name" & return & return & "Please click on an action!" buttons {"Rename", "Overwrite", "Ignore"}
			if buttonReturned is "Overwrite" then
				return (duplicate file linkPath to folder myFolderPath with replacing) as alias
			else if buttonReturned is "Rename" then
				display dialog "Enter Name:" buttons {"OK"} default button 1 default answer ""
				set newName to text returned of result
				if newName does not end with linkFileExtension then set newName to newName & "." & linkFileExtension
				set sourceFile to quoted form of POSIX path of link
				set destinationFile to quoted form of (POSIX path of myFolderPath & newName)
				do shell script "/bin/cp -p " & sourceFile & space & destinationFile
				return (myFolderPath & newName) as alias
			else
				return missing value
			end if
		else
			return (duplicate file link to folder myFolderPath) as alias
		end if
	end tell
end processLink

After trying very hard I am able to resolve rename issue but still there are some problem, since I renamed the file so link get broken so some time it copies the file and sometime not.

And if we copied on images many times into indesign document at that time also it get stopped. In the case of ignore i don’t want any operation.


set myFolderPath to "Macintosh:Poo_Test:Cam_475_IV:"
tell application "Adobe InDesign CS4"
	set myDoc to document 1
	tell myDoc
		repeat with oneLink in (get links)
			set linkPath to file path of oneLink as text
			display dialog linkPath
			if linkPath is not (myFolderPath & name of oneLink) then
				tell application "Finder"
					try
						set movedFile to (duplicate file linkPath to folder myFolderPath)
					on error
						display dialog "File  already exist with that name" & return & linkPath & return & "Please click on an action!" buttons {"Rename", "Overwrite", "Ignore"}
						if button returned of result is "Overwrite" then
							set movedFile to (duplicate file linkPath to folder myFolderPath with replacing) as alias
						else if button returned of result is "Rename" then
							display dialog "Enter Name:" buttons {"OK"} default button 1 default answer ""
							set renameFile to text returned of result
							set name of file linkPath to renameFile
							set movedFile to (duplicate file linkPath to folder myFolderPath) as alias
						end if
					end try
				end tell
				relink (contents of oneLink) to movedFile
				update oneLink
			end if
		end repeat
		
	end tell
end tell

Sorry Stefen, I have not seen your code I will check and get back to you.

Hi Stefen,
I have tested this and working fine as expected and many thanks for this. I have only two concern:

  1. If the one image is copied and pasted to multiple place in indesign document, then it is asking for rename those many times. My concern if one image placed many time from the same location into indesign then it should ask for only one time rename.

  2. In Ignore case image should get link from “set myFolderPath to “Macintosh:Poo_Test:Cam_475_IV:”” this location only.

Regards,
Poo

Hi Stefen,
I have done for ignore part i.e Issue 2. Can you please help me on handling issue 1.


property myFolderPath : "Macintosh:Poo_Test:Cam_475_IV:"

tell application "Adobe InDesign CS4"
	set myDoc to document 1
	tell myDoc
		repeat with oneLink in (get links)
			set linkPath to file path of oneLink
			if linkPath is not (myFolderPath & name of oneLink) then
				set movedFile to my processLink(linkPath)
				if movedFile is not missing value then
					relink (contents of oneLink) to movedFile
					update oneLink
				end if
			end if
		end repeat
	end tell
end tell

on processLink(link)
	tell application "Finder"
		tell file link
			set linkFileName to its name
			set linkFolderPath to its container as text
			set linkFileExtension to name extension
		end tell
		
		if exists file (myFolderPath & linkFileName) then
			set {button returned:buttonReturned} to display dialog ¬
				"File already exist with that name" & return & return & "Please click on an action!" buttons {"Rename", "Overwrite", "Ignore"}
			if buttonReturned is "Overwrite" then
				return (duplicate file linkPath to folder myFolderPath with replacing) as alias
			else if buttonReturned is "Rename" then
				display dialog "Enter Name:" buttons {"OK"} default button 1 default answer ""
				set newName to text returned of result
				if newName does not end with linkFileExtension then set newName to newName & "." & linkFileExtension
				set sourceFile to quoted form of POSIX path of link
				set destinationFile to quoted form of (POSIX path of myFolderPath & newName)
				do shell script "/bin/cp -p " & sourceFile & space & destinationFile
				return (myFolderPath & newName) as alias
			else
				---need to write code for ignore
				return (myFolderPath & linkFileName) as alias
				--return missing value
			end if
		else
			return (duplicate file link to folder myFolderPath) as alias
		end if
	end tell
end processLink

Regards,
Poo

In the case of Overwrite it is showing below error:


			if buttonReturned is "Overwrite" then
				return (duplicate file linkPath to folder myFolderPath with replacing) as alias

Then I changed code like:


			if buttonReturned is "Overwrite" then
				return (duplicate file link to folder myFolderPath with replacing) as alias

But the images are not getting linked.

Regards,
Poo

But they should getting linked. I’ve just tested it.

I don’t know yet to solve your other problem.
Probably you must go thru the link list with a repeat loop
every time after renaming an image to find the other occurrences.

Hi Stefen,
Thanks for the reply. I will check for re-lining part.
As far as other concern I am trying like if images are having the same path then it will ask for one time re-name rest exit like that.

I am trying as well and let you know once it is finished and in the mean time if you are getting any idea then please let me know.

Regards,
Poo