• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Help with find text command
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Help with find text command


  • Subject: Re: Help with find text command
  • From: "Wallace, William" <email@hidden>
  • Date: Tue, 07 Aug 2007 14:23:17 -0500
  • Thread-topic: Help with find text command

Just for kicks, here is the full text of the script I ended up with. It did
what I needed it to in a reasonable amount of time so I am happy with it,
but I'd also be curious to hear any comments or critiques from the
"bake-off" crew (or anybody else who cares to comment). I'm always glad to
leaern how I could do things better.

--
tell application "Adobe InDesign CS2"
    set theStoryList to every story of document 1
    set processCount to 0
    repeat with s from (count of theStoryList) to 1 by -1
        set aStoryRef to item s of theStoryList
        set theStory to text of aStoryRef
        if theStory is not {""} then
            set foundISBNs to my findISBNs(theStory)
        end if
        if foundISBNs is false then
            --do nothing, go on to next storyRef
        else
            repeat with i from (count of foundISBNs) to 1 by -1
                set anISBNrec to item i of foundISBNs
                set theISBN10 to matchResult of anISBNrec
                set theStartOffset to (matchPos of anISBNrec) + 1
                set theStopOffset to (theStartOffset + (matchLen of
anISBNrec))
                set AppleScript's text item delimiters to ""
                set {oldTIDs, AppleScript's text item delimiters} to
{AppleScript's text item delimiters, "-"}
                set isbnChunks to (text items of theISBN10)
                set AppleScript's text item delimiters to oldTIDs
                set rawISBN to isbnChunks as string
                set thePrice to my priceLookup(rawISBN)
                set newCheckDigit to my checkDigitThirteen("978" & (text 1
thru 9 of rawISBN) as string)
                set theISBN13 to "978-" & (item 1 of isbnChunks) & "-" &
(item 2 of isbnChunks) & "-" & (item 3 of isbnChunks) & "-" & newCheckDigit
                set insertionString to theISBN10 & tab & theISBN13 & tab &
thePrice
                set selection to text from character theStartOffset to
character theStopOffset of aStoryRef
                set contents of selection to insertionString
                set processCount to processCount + 1
            end repeat
        end if
    end repeat
    display dialog "Finished processing. " & processCount & " ISBNs
updated."
end tell

on priceLookup(rawISBN) --expects 10 digit ISBN as string with hyphens
removed
    tell application "Microsoft Excel"
        try
            set dataCells to used range of active sheet
            set isbnLocation to find dataCells what rawISBN
            set rownum to (first row index) of isbnLocation
            set theCellLocation to "C" & rownum
            set newPrice to string value of cell theCellLocation
        on error errMsg number errNum
            if errNum is -1708 then
                set newPrice to "$0.00"
            else
                display dialog "ERROR: " & errNum & return & errMsg
            end if
        end try
    end tell
    return newPrice
end priceLookup

on findISBNs(theText)
    try
        set matchedStrings to find text
"\\<[0-9]{5}-[0-9]{3}-[0-9]-[0-9Xx]\\>|\\<[0-9]{5}-[0-9]{2}-[0-9]{2}-[0-9Xx]
\\>|\\<[0-9]{5}-[0-9]-[0-9]{3}-[0-9Xx]\\>|\\<[0-9]{4}-[0-9]{4}-[0-9]-[0-9Xx]
\\>|\\<[0-9]{4}-[0-9]{3}-[0-9]{2}-[0-9Xx]\\>|\\<[0-9]{4}-[0-9]{2}-[0-9]{3}-[
0-9Xx]\\>|\\<[0-9]{4}-[0-9]-[0-9]{4}-[0-9Xx]\\>|\\<[0-9]{3}-[0-9]{5}-[0-9]-[
0-9Xx]\\>|\\<[0-9]{3}-[0-9]{4}-[0-9]{2}-[0-9Xx]\\>|\\<[0-9]{3}-[0-9]{3}-[0-9
]{3}-[0-9Xx]\\>|\\<[0-9]{3}-[0-9]{2}-[0-9]{4}-[0-9Xx]\\>|\\<[0-9]{3}-[0-9]-[
0-9]{5}-[0-9Xx]\\>|\\<[0-9]{2}-[0-9]{6}-[0-9]-[0-9Xx]\\>|\\<[0-9]{2}-[0-9]{5
}-[0-9]{2}-[0-9Xx]\\>|\\<[0-9]{2}-[0-9]{4}-[0-9]{3}-[0-9Xx]\\>|\\<[0-9]{2}-[
0-9]{3}-[0-9]{4}-[0-9Xx]\\>|\\<[0-9]{2}-[0-9]{2}-[0-9]{5}-[0-9Xx]\\>|\\<[0-9
]{2}-[0-9]-[0-9]{6}-[0-9Xx]\\>|\\<[0-9]-[0-9]{7}-[0-9]-[0-9Xx]\\>|\\<[0-9]-[
0-9]{6}-[0-9]{2}-[0-9Xx]\\>|\\<[0-9]-[0-9]{5}-[0-9]{3}-[0-9Xx]\\>|\\<[0-9]-[
0-9]{4}-[0-9]{4}-[0-9Xx]\\>|\\<[0-9]-[0-9]{3}-[0-9]{5}-[0-9Xx]\\>|\\<[0-9]-[
0-9]{2}-[0-9]{6}-[0-9Xx]\\>|\\<[0-9]-[0-9]-[0-9]{7}-[0-9Xx]\\>" in theText
with regexp and all occurrences
    on error errMsg number errNum
        if errNum is 1 then
            set matchedStrings to false
        else
            display dialog "ERROR: " & errNum & return & errMsg
            return false
        end if
    end try
    return matchedStrings
end findISBNs

on checkDigitThirteen(theRawString) --expects the first 12 digits of the 13
digit ISBN as a string (with hyphens stripped out)
    set {oldTIDs, AppleScript's text item delimiters} to {AppleScript's text
item delimiters, ""}
    set theDigitList to text items of theRawString
    set AppleScript's text item delimiters to oldTIDs
    set theSum to 0
    repeat with i from 1 to 12
        set theSum to theSum + ((item i of theDigitList) * ((((i - 1) mod 2)
* 2) + 1))
    end repeat
    set theCheckDigit to (10 - (theSum mod 10)) mod 10
    return theCheckDigit
end checkDigitThirteen
--

Thanks again, guys.
--
B!ll
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Help with find text command
      • From: Philip Aker <email@hidden>
References: 
 >Re: Help with find text command (From: Philip Aker <email@hidden>)

  • Prev by Date: Re: How to determine whether an event has an alarm
  • Next by Date: Re: Help with find text command
  • Previous by thread: Re: Help with find text command
  • Next by thread: Re: Help with find text command
  • Index(es):
    • Date
    • Thread