Shorten File Name

I need a way to shorten a filename. For example “xxx_xxx_filename_xxx” I need to extract out the “filename”, but the xxx can change in character size so I can’t use a simple count space script. There are too many variables. The only part that will stay consistant is the order. Any thoughts out there. This is for Indesign CS3. I had an idea of using the File Info information to put the filename in there under document name and have the script refer to that, but I can’t seem to get it to reconize it.

Thanks
Tim

Would there ever be more underscores than the three in your example?

No always 3 underscores in the same order

Give that a whirl

set full_filename to "xxx_xxx_filename_xxx"
set {TID, text item delimiters} to {text item delimiters, "_"}
set the_filename to text item 3 of full_filename
set text item delimiters to TID

James’ script should cover that then.

Just for future reference, if “filename” might contain underscores, then you could try something like this:

set example to "xxx_xxx_file_name_xxx"

set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to "_"
set exampleName to "" & text items 3 thru -2 of example
set AppleScript's text item delimiters to prevTIDs

exampleName
--> "file_name"

well played Bruce =)

Thank Guys

Works great on its own, but as soon as I add it to my full script, I get an error
"Adobe InDesign CS3 got an error: Can’t get text item 3 of document “reg_000_Doctor_nb11.indd”

arghhh

Tim

Can you post the script please?

Here is the script…

tell application "Finder"
	set issue_number to text returned of (display dialog "Enter the Issue Number" default answer "00") as string
	
	set region_list to {"Nation", "California", "Canada", "Florida", "North East", "North West", "Mid West", "South Atlantic", "South West"}
	
	set region_code to choose from list region_list with prompt "Choose a Region:"
	
	if region_code = {"Nation"} then
		set new_code to "CO"
	end if
	if region_code = {"Canada"} then
		set new_code to "CAN"
	end if
	if region_code = {"Florida"} then
		set new_code to "FL"
	end if
	if region_code = {"North East"} then
		set new_code to "NEA"
	end if
	if region_code = {"North West"} then
		set new_code to "NW"
	end if
	if region_code = {"Mid West"} then
		set new_code to "MW"
	end if
	if region_code = {"South Atlantic"} then
		set new_code to "SAT"
	end if
	if region_code = {"South West"} then
		set new_code to "SW"
	end if
end tell


tell application "Adobe InDesign CS3"
	activate
	set doc_name to active document
	set doc_pages to pages of doc_name
	
	set pdf_style to name of PDF export presets
	set export_style to (choose from list pdf_style with prompt "Select PDF Export Preset") as string
	set folder_path to (choose folder with prompt "Select folder to Save PDF files") as string
end tell

tell application "Finder"
	activate
	set delim_name to doc_name
	set prevTIDs to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "_"
	set the_filename to text item 3 of delim_name
	set AppleScript's text item delimiters to prevTIDs
	set the_filename to delim_name
	
	tell application "Adobe InDesign CS3"
		activate
		repeat with anItem in doc_pages
			set page_number to name of anItem as string
			
			count characters of page_number
			if length of page_number is 1 then
				set new_number to text -3 thru -1 of ("00" & page_number)
			end if
			if length of page_number is 2 then
				set new_number to text -3 thru -1 of ("0" & page_number)
			end if
			if length of page_number is 3 then
				set new_number to page_number
			end if
			
			set PDF_name to folder_path & new_code & "_" & new_number & the_filename & "_nb" & issue_number & ".pdf"
			set page range of PDF export preferences to page_number
			
			--set page range of PDF export preferences to "Sec1:" & page_number
			
			tell doc_name
				export format PDF type to PDF_name using export_style without showing options
			end tell
		end repeat
	end tell
end tell

beep
display dialog "Your PDFs are Done" buttons {"Done"} default button 1

The problem is this line here

set doc_name to active document

doc_name is being set to the document itself not the name. So change the line to read as

set doc_name to name of active document

and you should be all set.

Hi,

doc_name is the active document, not it’s name
The Finder is not needed at all to use text item delimiters.

Here an optimized version of your script, I guess the code of Calfornia in ZIP_list :wink:


set issue_number to text returned of (display dialog "Enter the Issue Number" default answer "00") as string

set region_list to {"Nation", "California", "Canada", "Florida", "North East", "North West", "Mid West", "South Atlantic", "South West"}
set ZIP_list to {"CO", "CA", "CAN", "FL", "NEA", "NW", "MW", "SAT", "SW"}

set region_code to choose from list region_list with prompt "Choose a Region:"
tell result
	if it is false then return
	set region_code to item 1 of it
end tell

if region_code is false then return
repeat with i from 1 to count region_list
	if item i of region_list is region_code then exit repeat
end repeat
set new_code to item i of ZIP_list


tell application "Adobe InDesign CS3"
	activate
	set doc_name to active document
	set doc_pages to pages of doc_name
	
	set pdf_style to name of PDF export presets
	set export_style to (choose from list pdf_style with prompt "Select PDF Export Preset") as string
	set folder_path to (choose folder with prompt "Select folder to Save PDF files") as string
	
	set the_filename to name of doc_name
	set prevTIDs to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "_"
	set the_filename to text item 3 of the_filename
	set AppleScript's text item delimiters to prevTIDs
	
	repeat with anItem in doc_pages
		set new_number to text -3 thru -1 of ("00" & name of anItem)
		set PDF_name to folder_path & new_code & "_" & new_number & the_filename & "_nb" & issue_number & ".pdf"
		set page range of PDF export preferences to page_number
		
		--set page range of PDF export preferences to "Sec1:" & page_number
		
		tell doc_name
			export format PDF type to PDF_name using export_style without showing options
		end tell
	end repeat
end tell

beep
display dialog "Your PDFs are Done" buttons {"Done"} default button 1


Nitpick: That string coercion is not needed.

Thanks Stefan, it works great
It will save us alot of time and keep all file consistantly name properly.

Thanks again for everyones help.

I’m learning more and more each time I attempt a new script…and lovin it.

Thanks Tim :smiley: