• 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: collectdata
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: collectdata


  • Subject: Re: collectdata
  • From: Julien Battist <email@hidden>
  • Date: Mon, 13 Mar 2017 16:24:09 +0100
  • Importance: normal
  • Sensitivity: Normal

Hi Shane, first of all this looks pretty stable and returns are colored nicely. Many thanks for this code which is still hard to read for me :)
 
Coming back on regular expressions I would appreciate some feedback on the following.
In indesign I can grab data in a 'cell of a kolom' using a regular _expression_ where the first 5 characters are unique and the rest is grabbed being data in the same cell.
 
In a pdf this is separated with a space or a tab or even with nothing.
 
So what I want to grab is the following in bold: 
ABCD-123abc
Some text ABCD-abc234 some other text
ABCD-76y65yj90
ABCD-76yABC90 some text ABCD-code6547AA
----
 
ABCD- is fixed the rest can be random. What would be the right regular _expression_ for this?
 
Many thanks for any feedback,
Julien
 
 
Sent: Friday, March 10, 2017 at 1:51 AM
From: "Shane Stanley" <email@hidden>
To: "AS users" <email@hidden>
Subject: Re: collectdata
On 9 Mar 2017, at 7:44 pm, Julien Battist <email@hidden> wrote:
 
Yes, I also had some strange hits using Shane's code....
 
There does seem to be a bug in how bounds are returned from characterBoundsAtIndex:. This version uses a different method:
 
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "Quartz" -- for PDF stuff
use scripting additions
 
set thePath to POSIX path of (choose file of type {"pdf"})
-- define search pattern
set {theRegex, theError} to current application's NSRegularExpression's regularExpressionWithPattern:"\\d+" options:0 |error|:(reference)
-- make URL
set anNSURL to current application's |NSURL|'s fileURLWithPath:thePath
-- make destination URL
set oldName to anNSURL's lastPathComponent()'s stringByDeletingPathExtension()
set newName to oldName's stringByAppendingString:"-2.pdf"
set destURL to anNSURL's URLByDeletingLastPathComponent()'s URLByAppendingPathComponent:newName
-- open doc and count pages
set theDoc to current application's PDFDocument's alloc()'s initWithURL:anNSURL
set theCount to theDoc's pageCount() as integer
-- create list to hold results
set findInfo to {}
-- loop through pages
repeat with i from 1 to theCount
-- get page and its text
set thePage to (theDoc's pageAtIndex:(i - 1))
set theText to thePage's |string|()
-- do search
set theRanges to (theRegex's matchesInString:theText options:0 range:{0, theText's |length|()})
-- create list to hold contents of matches
set pageFinds to {}
-- loop through matches found
repeat with aFind in theRanges
-- get the range and start and finish indexes
set foundRange to aFind's range()
-- get the matched text
set end of pageFinds to (theText's substringWithRange:foundRange) as text
-- get bounds of the matched text
set theSelection to (thePage's selectionForRange:foundRange)
set theBounds to (theSelection's boundsForPage:thePage)
-- make highlight annotation
set theHighlight to (current application's PDFAnnotationMarkup's alloc()'s initWithBounds:theBounds)
(theHighlight's setMarkupType:(current application's kPDFMarkupTypeHighlight))
-- add to page
(thePage's addAnnotation:theHighlight)
end repeat
-- update result
set end of findInfo to {pageNumber:i, foundStrings:pageFinds}
end repeat
-- save new PDF
theDoc's writeToURL:destURL
-- return result
return findInfo
 
-- 
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>, <latenightsw.com>
 

 _______________________________________________
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: collectdata
      • From: Shane Stanley <email@hidden>
    • Re: collectdata
      • From: Jim Underwood <email@hidden>
    • Re: collectdata
      • From: "Jacopille, David" <email@hidden>
References: 
 >collectdata (From: Julien Battist <email@hidden>)
 >Re: collectdata (From: Shane Stanley <email@hidden>)
 >Re: collectdata (From: Thomas Fischer <email@hidden>)
 >Re: collectdata (From: Julien Battist <email@hidden>)
 >Re: collectdata (From: Yvan KOENIG <email@hidden>)
 >Re: collectdata (From: Christopher Stone <email@hidden>)
 >Re: collectdata (From: Thomas Fischer <email@hidden>)
 >Re: collectdata (From: Julien Battist <email@hidden>)
 >Re: collectdata (From: Shane Stanley <email@hidden>)
 >Re: collectdata (From: Thomas Fischer <email@hidden>)
 >Re: collectdata (From: Shane Stanley <email@hidden>)
 >Re: collectdata (From: Shane Stanley <email@hidden>)

  • Prev by Date: Re: Differences in output between AppleScript and Terminal
  • Next by Date: Re: collectdata
  • Previous by thread: Re: collectdata
  • Next by thread: Re: collectdata
  • Index(es):
    • Date
    • Thread