Difference between numbers and letters

Does Applescript knows the difference between a letter and a number in a string? I want applescript to look at the name of 2 different files, and if the numbers in each file names are different then display a message.

I could go by the characters (example 6 through 8) but my problem is that the length of my file names are differents for each file, but they are 3 digits that needs to be the same in both files.

Model: iMac
AppleScript: 2.3
Browser: Safari 533.17.8
Operating System: Mac OS X (10.6)

If the file only contains three digits you could use egrep to extract the digits from the file name and compare these two strings.

set nameOfFirstFile to "blahblahblah456.jpg"
set nameOfSecondFile to "123blahblahblah.kpg"

set x to do shell script "/bin/echo -n " & quoted form of nameOfFirstFile & " | egrep -o '[[:digit:]]{1,}'" --result:"456"
set y to do shell script "/bin/echo -n " & quoted form of nameOfSecondFile & " | egrep -o '[[:digit:]]{1,}'"--result:"123"

set filesAreEqual to x = y

It would be more useful for us all of you’re more specific with example names to compare…

it’s almost just digits!

file 1: 123456_005_01.pdf
file 2: 123456_005_01_Fiches.pdf

it’s the (01) that as to be the same on both files, if they are different an alert message will pop-up.

Unless I misunderstand your question, I dont see why you dont just open the files as alias types, then coerce
them into text type variables, and then do a string comparison.
Any difference in the text strings will be checkable, something like this works on my computer.

[code]set file1 to “Macintosh HD:Users:YourName:Documents:123456_005_01.pdf” as alias
set file2 to “Macintosh HD:Users:YourName:Documents:123456_005_01_Fiches.pdf” as alias

tell application “System Events”
set file1_name to name of file1 as text
set file2_name to name of file2 as text
end tell

set files_are_same to (file1_name is equal to file2_name) as boolean[/code]
Hope this helps

Regards Mark

Hi. In regards to your original question”is there a difference between letters and numbers in a string”the answer is that everything within a string is in the text class. Your example indicates that you’re only concerned with the final two digits, which are words. If your naming convention remains consistent, something along these lines might work.


property theNumbers : ({0} & {123456789} as text)'s items--creates a list of characters to test against

set file1 to "123456_005_01.pdf"
set file2 to "123456_004_01_Fiches.pdf"
-----------------------------------------------------------------------------------------------------
--removes name extensions, if present
set AppleScript's text item delimiters to ".pdf"
set {file1, file2} to {file1's text item 1, file2's text item 1}
set AppleScript's text item delimiters to ""
-----------------------------------------------------------------------------------------------------
finalsequence(file1) = finalsequence(file2)

on finalsequence(this)
	tell this to if word -1's character 1 is in theNumbers then
		word -1
	else
		word -3
	end if
end finalsequence

This would do it. It assumes all file names have a 3 letter file extension and “_” is always the separator character of the numbers in the file name.

set f1 to "123456_005_01.pdf"
set f2 to "123456_005_01_Fiches.pdf"

-- trim the file extensions from the file names
set f1 to text 1 thru -5 of f1
set f2 to text 1 thru -5 of f2

-- separate the file names into a list using "_" as the separator
set text item delimiters to "_"
set fx1 to text items of f1
set fx2 to text items of f2
set text item delimiters to ""

-- find all the numbers in the first file name
set f1List to {}
repeat with i from 1 to count of fx1
	set listItem to item i of fx1
	try
		set end of f1List to listItem as number
	end try
end repeat

-- find all the numbers in the second file name
set f2List to {}
repeat with i from 1 to count of fx2
	set listItem to item i of fx2
	try
		set end of f2List to listItem as number
	end try
end repeat

-- compare the last numbers in each list and show the result
if f1List is {} then
	set theMessage to "The following file name does not have a number in it: " & f1
else if f2List is {} then
	set theMessage to "The following file name does not have a number in it: " & f2
else
	set f1Number to item -1 of f1List
	set f2Number to item -1 of f2List
	
	if f1Number is f2Number then
		set theMessage to "The last numbers are the same"
	else
		set theMessage to "The last numbers are different"
	end if
end if

display dialog theMessage

sbriss needs to be clear whether he means three digits (as specified in post #1) or two (as in the “01” mentioned in post #3). Better still, since there are several groups of digits in each name and he only gives two examples, he should say how the relevant group is identified. Is it the third group? The last of however many there may happen to be? The one with two (or three) digits? All of them (as in the examples)? Something else? Not just “it’s the (01) that as to be the same”.

His meaning is not exactly clear, however this is what I took it to mean.

Sorry if I was wasn’t clear!

I think I will be more clear of my intentions! Here it is:

I have Cover of Books and Library Cards that goes with the books for a Library. I created a scrtipt for Indesign to mount them in the same document for Archive purpose.

The script ask the user to choose the PDF file for the Cover, and then choose de Library card that goes with it.

My only concern is that the user doesn’t choose the right Library card that goes with the cover. Next year when we’ll reprint the books, i don’t want to have a case where the library card doesn’t fit with the book.

The file naming are all like my example.

example 1: cover file: 123456_001_01.pdf Library Card: 123456_001_01_Fiches.pdf
example 2: cover file: 123456_001_02.pdf Library Card: 123456_001_02_Fiches.pdf
example 3: cover file: 123456_001_03.pdf Library Card: 123456_001_03_Fiches.pdf
example 4: cover file: 345692_009_02.pdf Library Card: 345692_009_02_Fiches.pdf

123456 being my docket number
001 is the lot number in my docket
01 is the book number in my lot, in my docket

The book number will always be 2 digits number because we usually have 10 to 15 books per lot.

So the an alert window must pop-up if the user doesn’t choose the right library card.

Thanks for clarifying. :slight_smile:

It sounds safest ” and is certainly easiest ” to compare the whole names, apart from the “_Fiches” and perhaps the “.pdf” parts:

set coverFileName to "123456_001_01.pdf"
set libraryCardName to "123456_001_01_Fiches.pdf"

set OK to (libraryCardName begins with text 1 thru -5 of coverFileName)

I presume there’s no way the script could find the associated library card for itself?

OK, you want to compare all the numbers not just the last number. So that’s an easy modification to the script I previously wrote. You just change the last section to check all the numbers. If any of the numbers do not match this script will tell you.

set f1 to "123456_005_01.pdf"
set f2 to "123456_005_01_Fiches.pdf"

-- trim the file extensions from the file names
set f1 to text 1 thru -5 of f1
set f2 to text 1 thru -5 of f2

-- separate the file names into a list using "_" as the separator
set text item delimiters to "_"
set fx1 to text items of f1
set fx2 to text items of f2
set text item delimiters to ""

-- find all the numbers in the first file name
set f1List to {}
repeat with i from 1 to count of fx1
	set listItem to item i of fx1
	try
		set end of f1List to listItem as number
	end try
end repeat

-- find all the numbers in the second file name
set f2List to {}
repeat with i from 1 to count of fx2
	set listItem to item i of fx2
	try
		set end of f2List to listItem as number
	end try
end repeat

-- compare all the numbers in each list and show if they all match or not
set f1Count to count of f1List
set f2Count to count of f2List
if f1Count is not f2Count then
	set theMessage to "The numbers do not match."
else
	repeat with i from 1 to f1Count
		if (item i of f1List) is not equal to (item i of f2List) then
			set theMessage to "The numbers do not match."
			exit repeat
		else if i is equal to f1Count then
			set theMessage to "The numbers match."
			exit repeat
		end if
	end repeat
end if

display dialog theMessage

There is a way for everything! :wink:

I will try to continue developing that little script there is surely a way to do this.

Thank you, I really appreciate that little line! Reducing error margins!

SB

to Nigel

Finally i found a way for the script to look for the library card that goes with the cover. As long as both pdf’s are in the same folder.

Just added these lines

	tell application "Finder"
		set MyFolder to folder of Couverture as string
		set NomCouverture to get name of Couverture as string
		set NomNoExtension to (text 1 thru -5 of NomCouverture) as string
                    set maCarteBiblio to (MyFolder & NomNoExtension & "_Fiche.pdf")
	end tell

Thank’s a lot for your help :slight_smile:

To all the others who replied to my thread.