sticky conditional or, getting digits from file name

Hi Folks-

I’m trying to pull the file number out of an InDesign file name. Typically, the files are named “83245_This Document” or “122134 ThisDocument.indd”

Since the number of digits isn’t fixed, I thought I could step through the string one character at a time, look for a non-integer and use this to break out of the loop. Then, (I haven’t coded this yet) use the number of digits generated to pull the correct number of digits out of the file name.

I keep getting the error ‘expected end of line, but got “repeat”’

on run
	tell application "Adobe InDesign CS2"
		set vdocName to name of front document as string
	end tell
	set vincrement to 1
	set vconditionMet to false
	repeat while vconditionMet is false
		set vcomparenumber to character vincrement of vdocName as string
		set vincrement to vincrement + 1
		if class of valueEntered does not equal integer then
		set vconditionMet to true
	end repeat
end run

thanks,

Ralph

Try something like this:

tell application "Adobe InDesign CS2"
	set vdocName to name of front document
end tell

do shell script "echo " & quoted form of vdocName & " | /usr/bin/grep -o '^[0-9]*'"
set digits to result

Edit:

You need to change that if statement to one of these:

if class of valueEntered does not equal integer then
	set vconditionMet to true
end if

-- or
if class of valueEntered does not equal integer then set vconditionMet to true

Also, I don’t see where valueEntered is defined in your script.

Edit:

Side note: You don’t need to coerce values into a string when they are already a string; It’s pointless. Also, you could also be downgrading Unicode text into a string, which shouldn’t be done unless you have a reason for doing it.

Hi Ralph,

I would use an try block to force an error, if the string cannot be coerced to an integer

tell application "Adobe InDesign CS2"
	set vdocName to name of front document
end tell
set theName to ""
repeat with i in (get characters of vdocName)
	try
		(theName & i) as integer
		set theName to theName & i
	on error
		exit repeat
	end try
end repeat

Also, text is always text, no matter what it looks like. You need to try and coerce each character to an integer (which StefanK hinted at).

This should also work:

set vdocName to "122134 ThisDocument.indd"

set digits to ""
count vdocName

repeat with i from 1 to result
	try
		tell character i of vdocName
			it as integer -- This will throw an error for non-numeric characters
			set digits to digits & it
		end tell
	on error
		exit repeat
	end try
end repeat

digits

Edit: To clarify my first sentence:

class of "1"
--> string

Hi Ralph,

Your script is almost there. You don’t need the condition met stuff. Just keep popping (maybe storing might be a better word) the numbers from the string until you don’t get a number. Note that “” as integer is 0.


on run
	set char_list to "0123456789"
	set vdocName to "1234_hello"
	(*
    tell application "Adobe InDesign CS2" 
        set vdocName to name of front document as string 
    end tell 
*)
	set vincrement to 0
	set num_list to {}
	repeat
		set vincrement to vincrement + 1
		set vcomparenumber to character vincrement of vdocName
		if vcomparenumber is in char_list then
			set end of num_list to vcomparenumber
		else
			exit repeat
		end if
	end repeat
	set num_string to num_list as string as integer
end run

gl,

Bruce- the shell script would fail if the file name were named “34521_NewDoc2.indd”, as it would return a result of 345212 instead of 34521, which is why I was trying to step through it.

Yes, I was missing a few variable names, but I wasn’t even getting the code to compile…

Ok, I read an article on Unicode and vaguely understand the concept- are you saying that the result I’m getting from:

set vdocName to name of front document as string

would be Unicode if I left off the ‘as string’? and if so, what format of text am I turning it into?

I understand the concept of forcing an error by using ‘(theName & i) as integer’

I’ll try that angle.

thanks all!

-Ralph

That expression starts at the being of the line and matches any numbers it find, until it finds something that is not a number.

set vdocName to "34521_NewDoc2.indd"

do shell script "echo " & quoted form of vdocName & " | /usr/bin/grep -o '^[0-9]*'"
set digits to result
--> "34521"

You’re right, of course.

thanks,

Ralph

:)This script now works. I’m sure it’s pretty ugly, and i need to finish commenting, but it’s fully functional, and installed on our studio macs. I’m going to make a few more tweaks in the future.

Only weird thing is, on one of the machines it throws an error “can’t get some object -1728”. I’ve got our IT guy investigating it to see if that machine has some different settings.

Thanks again for helping out a clueless newbie!

Ralph

set vmyDesktop to alias ((path to desktop as string))
tell application “Adobe InDesign CS2”
set vdocName to name of front document as string
end tell
do shell script “echo " & quoted form of vdocName & " | /usr/bin/grep -o ‘^[0-9]*’”
set vdigits to result
tell application “Finder”
set vLocationFile to ((vmyDesktop as string) & vdigits as string) & “_pp.pdf”

set vcompare to 10000 --starts with 10,000s folder
repeat
	set vcompare to vcompare + 1000 -- increments by 1000
	if vcompare > vdigits then exit repeat -- gets out of comparision loop when comparison number is 1000 greater than file number
	
end repeat
set vfolder to ((vcompare as integer) - 1000) -- subtracts 1000 from comparison number to get correct folder number to save to
set vtens to vfolder / 1000 as integer -- cuts off 3 zeros from folder number to prep number for comma addition
set vactualfolder to (vtens as string) & "," & "000" --adds comma and three zeros to make folder number user friendly and compatible with our system
set vfullpath to "Users:SHARED:_PDF Page Proofs (low res):PDF Page Proofs #" & vactualfolder -- takes partial path and adds correct thousands folder to complete path
set vpartialnewfoldername to "PDF Page Proofs #" -- sets up first part of folder name
set vnewfoldername to vpartialnewfoldername & vactualfolder --creates full folder name
vnewfoldername as string
tell application "Finder"
	if not (exists folder vfullpath) then --checks to see if folder exists
		make new folder at "Users:SHARED:_PDF Page Proofs (low res)" with properties {name:vnewfoldername} -- if no folder exists, creates new one
	end if
end tell
set vexport to ((vfullpath as string) & ":" & vdigits as string) & "_pp.pdf"

tell application "Adobe InDesign CS2"
	export document 1 format PDF type to vexport using PDF export preset "[Smallest File Size]" without showing options
	export document 1 format PDF type to vLocationFile using PDF export preset "[Smallest File Size]" without showing options
end tell

end tell