Help with a script please...

Hi there,

I’ve managed to create a script that almost does what I need but I’m not sure what would be the best way to tackle one last bit.

Basically, I have a Quark document that contains several pages. Each page has several pic boxes with pics in.

I’ve written a script that goes through the Quark document and creates a list of the pictures used within the document.
It only creates a list of the picture names that start with a number which is a unique id.

For example in the list below it would only make a list containing three of the four files, the file ‘abcd_pic3’ would not be included.

1234_pic1
1234i_pic2
abcd_pic3
5678_pic4

The list generated looks like this:-

1234,1234_pic1
1234,1234i_pic2
5678,5678_pic4

The numbers from the front of filename are listed and then the actual full filename seperated by a comma.

Notice in the first list there are two filenames that start with 1234 the difference being the ‘i’ after the number.
Here’s the question - when this occurs I want to take that filename and put it on the same line as the number 1234.
So the final list generated would look like:-

1234,1234_pic1,1234i_pic2
5678,5678_pic4

rather than:-

1234,1234_pic1
1234,1234i_pic2
5678,5678_pic4

Can anyone suggest the best way to go about this? I obviously need to look to see what the next filename is, so if the cuurent id is 1234 I need to look to the next filename to see if that starts with 1234. If it does I need to add it to the last line. I’ve done some work with Coldfusion lists and with that I have a List Append command, is there something similar with Applescript.

Any help would be appreciated, thanks in advance,

Nick

P.S. I know there are probably many ways my code could be cleaned up, I’m still a bit of a newbie to this.

Here’s the code:-


on open (someitems)
	
	set LogFile to a reference to (path to desktop as string) & "PicList_NamesOnly.txt"
	set savedTextItemDelimiters to AppleScript's text item delimiters
	set filecount to 0
	
	set picList to {}
	
	tell application "QuarkXPress"
		
		activate
		repeat with theitem in someitems
			open theitem remap fonts no use doc prefs yes
			get the count of every page in document 1
			set pagecount to the result
			set filename to (name of (get info for theitem))
			set picList to (picList & "FILENAME:- " & filename & return as text)
			
			repeat with pc from 1 to pagecount -- page loop
				set picBoxCount to the count of every picture box of page pc of document 1 -- pic box loop
				repeat with i from 1 to picBoxCount
					tell picture box i of page pc of document 1
						set fn to (file path of image 1) as text
						if fn is not equal to "«class null»" then
							set AppleScript's text item delimiters to ":"
							set ItemName to fn's text items -1 thru -1
							set AppleScript's text item delimiters to {""}
							
							set textItemName to ItemName as text
							
							-- ---------------------------------------------------------------------
							
							set char_count to 1
							set numChars to ""
							
							set char to character char_count of textItemName
							set av to ASCII number of char
							
							repeat while av is not less than 48 and av is not greater than 57
								set numChars to numChars & (character char_count of textItemName)
								set char_count to char_count + 1
								set char to character char_count of textItemName
								set av to ASCII number of char
							end repeat
							
							-- ---------------------------------------------------------------------
							
							if numChars is not equal to "" then
								set picList to picList & numChars & "," & ItemName & return as text
							end if
						end if
					end tell
				end repeat
			end repeat

			set picList to (picList & return as text)
			
			set total to the count of picList
			
			close document 1 saving no
			set filecount to filecount + 1
		end repeat
		
		set picList to picList & "Count:- " & filecount & return as text
		
	end tell
	
	try
		open for access file LogFile with write permission
		set eof of file LogFile to 0
		write picList to file LogFile
		close access file LogFile
		
	on error errText number errNum
		close access file LogFile
		display dialog errText
	end try
	
	set AppleScript's text item delimiters to savedTextItemDelimiters
	
end open

Sorry I don’t have time to go through your script at the moment but this does what you want, I think, at least once you have the picture names in an AppleScript list (which I’ve hardcoded at the top):

set list1 to {"1234_pic1", "1234i_pic2", "abcd_pic3", "5678_pic4"}

set list2 to {}
repeat with itm1 in list1
	set contnt1 to contents of itm1
	try
		set prefix to text 1 through 4 of contnt1
		try
			prefix as integer
			set areDigits to true
		on error
			set areDigits to false
		end try
		
		if areDigits then
			set found to false
			repeat with itm2 in list2
				if text 1 through 4 of item 1 of itm2 is prefix then
					set end of itm2 to contnt1
					set found to true
					exit repeat
				end if
			end repeat
			if not found then
				set end of list2 to {contnt1}
			end if
		end if
	end try
end repeat

repeat with itm2 in list2
	set beginning of itm2 to text 1 through 4 of item 1 of itm2
end repeat

--{{"1234", "1234_pic1", "1234i_pic2"}, {"5678", "5678_pic4"}}

This provides a list of two lists, one the “1234” list and the other the “5678” list. Hope that provides some help…

  • Dan

Thanks for the help Dan, I going to try and apply that to the script I have so far.

The type of list I was trying to achieve was like this:-

1234,1234_pic1 1234i_pic2
5678,5678_pic4

from this :-

1234_pic1
1234i_pic2
abcd_pic3
5678_pic4

As you can see it’s just the one list, I think, and where a pic name starting with the same digits is encountered the pic name is placed with the other as shown with ‘1234’ above.

If anyone else can add more to this I’d be grateful.

Thanks in advance,

Nick

Nick -

Guess I’m not sure what you mean by “list” if not the technical AppleScript term, but if you mean just a text string that “lists” the results then just a minor adjustment to the above script gives you those results:

set list1 to {"1234_pic1", "1234i_pic2", "abcd_pic3", "5678_pic4"}

set list2 to {}
repeat with itm1 in list1
	set contnt1 to contents of itm1
	try
		set prefix to text 1 through 4 of contnt1
		try
			prefix as integer
			set areDigits to true
		on error
			set areDigits to false
		end try
		
		if areDigits then
			set found to false
			repeat with itm2 in list2
				if text 1 through 4 of item 1 of itm2 is prefix then
					set end of itm2 to contnt1
					set found to true
					exit repeat
				end if
			end repeat
			if not found then
				set end of list2 to {contnt1}
			end if
		end if
	end try
end repeat

set str to ""
set {saveDelims, text item delimiters} to {text item delimiters, ", "}
repeat with itm2 in list2
	set beginning of itm2 to text 1 through 4 of item 1 of itm2
	set str to str & items of itm2 & return
end repeat
set text item delimiters to saveDelims
str
--"1234, 1234_pic1, 1234i_pic2
5678, 5678_pic4
"

Just for what it’s worth…

  • Dan