Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript - progressive loops get slower. Solutions?

Hi all,


I made a terrible Automator script and because it performed so badly (thanks to 'Watch Me Do') I eventually replaced every section with an Applescript action I wrote myself. My Applescript knowledge is limited so my scripts are rudimentary - but they work. My issue is that as it processes, each successive loop gets exponentially slower!


Here's what I'm tying to do:

We have 47 invoices with hundreds of lines of products in most of them. I have a list of product codes we should not have been charged and I need to ascertain if those codes appear on these invoices.


Here's my planned method:

  • Extract the entire (text) contents of each PDF invoice and (prefaced by the PDF filename so I can identify the guilty invoice) paste it into TextEdit for 'massaging' into spreadsheet-friendly formatting.
  • Copy and paste massaged content into Numbers and use the 'COUNTIF' function (or other suitable argument) to highlight instances of being charged

Here's my Automator script:

https://www.dropbox.com/s/wx90ryzle9mofp3/Extract%2520VSS%2520PDF%2520Invoice%25 20Contents.workflow.zip?dl=0

I know there are better more efficient scripts that will perform the same task better, but I'm keen to learn two things:

  1. Why does it get exponentially slower and how to prevent this for future scripts
  2. A more efficient way of getting this done. (Using Finder to search file contents of the enclosing folder doesn't work because the way the PDF's are constructed/formatted)

Any help or input is greatly appreciated.

Thanks!

Jeremy

MacBook Pro (Retina, Mid 2012), OS X Yosemite (10.10.5)

Posted on Feb 12, 2016 5:02 PM

Reply
Question marked as Best reply

Posted on Feb 12, 2016 7:57 PM

Are you getting the PDF into textEdit by copying and pasting (with an Applescript)

You may speed things up by using the Automator Action: Extract PDF Text

You could then put the text into a textEdit file in the next Action,

and then use grep to search for the instances of being charged

3 replies
Question marked as Best reply

Feb 12, 2016 7:57 PM in response to MrHuman

Are you getting the PDF into textEdit by copying and pasting (with an Applescript)

You may speed things up by using the Automator Action: Extract PDF Text

You could then put the text into a textEdit file in the next Action,

and then use grep to search for the instances of being charged

May 15, 2016 9:42 PM in response to MrHuman

This should do what you want.

Note that the text is extracted in rtf format then converted to txt (If converted by Automator to txt there are invisible char in the txt file that interfere with grep)

The rtf file is removed after it is processed.


In this example, grep is looking for foo or bar (foo|bar) in the files. (grep -E 'foo|bar'),

You can replace this with the product codes you're looking for (grep -E '000234|000235|000236')


The Exceptions will be put in a file names "Exceptions.txt" on the desktop.

The date and time run and the name of the PDF file will preface the exceptions.

The exceptions are appended to the file, so each time you run this, it will add to the end of the file, so remember to delete (or rename) the Exceptions.txt file if you want a fresh file when run.


User uploaded file


The Run Shell Script Action is:


for f in "$@"

do

echo -e "\n\n$(date)\n${f/%rtf/pdf}:\n" >> ~/Desktop/Exceptions.txt

textutil -cat txt -stdout "$f" | grep -E 'foo|bar' >> ~/Desktop/Exceptions.txt

rm "$f"

done

May 15, 2016 10:41 PM in response to Tony T1

Thanks Tony! I got around to testing this one some time after you posted it. - Talk about hitting the problem on the head with a sledge hammer! What an awesome little code! It worked beautifully, although because I found hidden iterations of some itemised and some non-itemised products in the PDF formatting, it confused the results so manually identifying products was still required.


You've peaked my interest in learning how to do Shell scripts now (as well as my Applescript studies). How did you learn? Any recommended sources?

Applescript - progressive loops get slower. Solutions?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.