Update AppleScript + Ruby script to work in High Sierra or Mojave

I have a few very useful scripts created by some extremely helpful members of this community.


Some of them utilise Ruby scripts, which has unfortunately changed since OSX 10.7. I've attempted to change the paths to the new ruby location (from version 2.0 to 2.3) but it still fails.


This one takes a multiple page PDF, searches for a text string on each page, and splits the file into individual files each named with the results of the string search.


This script works great in 10.7.5 but I can't get it to run in 10.13.6


Could someone help me to get Ruby working again?




_main()


on _main()


script o


property aa : choose file with prompt ("Choose PDF Files.") of type {"com.adobe.pdf"} ¬


default location (path to desktop) with multiple selections allowed



set my aa's beginning to choose folder with prompt ("Choose Destination Folder.") ¬


default location (path to desktop)



set args to ""


repeat with a in my aa


set args to args & a's POSIX path's quoted form & space


end repeat



considering numeric strings


if (system info)'s system version < "10.9" then


set ruby to "/usr/bin/ruby"


else


set ruby to "/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby"


end if


end considering



do shell script ruby & " <<'EOF' - " & args & "


require 'osx/cocoa'


include OSX


require_framework 'PDFKit'




outdir = ARGV.shift.chomp('/')




ARGV.select {|f| f =~ /\\.pdf$/i }.each do |f|


    url = NSURL.fileURLWithPath(f)


    doc = PDFDocument.alloc.initWithURL(url)


    path = doc.documentURL.path


    pcnt = doc.pageCount


    


    (0 .. (pcnt - 1)).each do |i|


        page = doc.pageAtIndex(i)


       page.string.to_s =~ /umber: \\S*/


        name = $&


        unless name


            puts \"no matching string in page #{i + 1} of #{path}\"


            next # ignore this page



        end


newname = name[7..-1]


        doc1 = PDFDocument.alloc.initWithData(page.dataRepresentation) # doc for this page


        unless doc1.writeToFile(\"#{outdir}/#{newname}.pdf\")


            puts \"failed to save page #{i + 1} of #{path}\"


        end


    end


end


EOF"


end script


tell o to run


end _main

Posted on Mar 6, 2019 2:38 AM

Reply
Question marked as Top-ranking reply

Posted on Mar 6, 2019 3:12 AM

Hi,


utilising yet another script from the awesome Hiroto, I managed to use Python instead - thi sis the new script:


_main()


on _main()


script o


property aa : choose file with prompt ("Choose PDF Files.") of type {"com.adobe.pdf"} ¬


default location (path to desktop) with multiple selections allowed



set my aa's beginning to choose folder with prompt ("Choose Destination Folder.") ¬


default location (path to desktop)



set args to ""


repeat with a in my aa


set args to args & a's POSIX path's quoted form & space


end repeat




do shell script "/usr/bin/python <<'EOF' - " & args & "


# coding: utf-8


import sys, re


from Foundation import NSURL


from Quartz.PDFKit import PDFDocument




argv = [ a.decode('utf-8') for a in sys.argv[1:] ]


outdir = argv.pop(0).rstrip('/')


 


for f in [ a for a in argv if re.search(r'\\.pdf$', a, re.I) ]:


    url = NSURL.fileURLWithPath_(f)


    doc = PDFDocument.alloc().initWithURL_(url)


    path = doc.documentURL().path()


    pcnt = doc.pageCount()


  


    for i in range(0, pcnt):


        page = doc.pageAtIndex_(i)


        m = re.search(r'number: \\S*', page.string())


        if not m:


            print 'no matching string in page %d of %s' % (i + 1, path.encode('utf-8'))


            continue    # ignore this page


        name = m.group()


name = name.replace('number: ','')


        doc1 = PDFDocument.alloc().initWithData_(page.dataRepresentation()) # doc for this page


        if not doc1.writeToFile_('%s/%s.pdf' % (outdir, name)):


            print 'failed to save page %d of %s' % (i + 1, path.encode('utf-8'))


EOF"


end script


tell o to run


end _main

Similar questions

2 replies
Question marked as Top-ranking reply

Mar 6, 2019 3:12 AM in response to Phillip Briggs

Hi,


utilising yet another script from the awesome Hiroto, I managed to use Python instead - thi sis the new script:


_main()


on _main()


script o


property aa : choose file with prompt ("Choose PDF Files.") of type {"com.adobe.pdf"} ¬


default location (path to desktop) with multiple selections allowed



set my aa's beginning to choose folder with prompt ("Choose Destination Folder.") ¬


default location (path to desktop)



set args to ""


repeat with a in my aa


set args to args & a's POSIX path's quoted form & space


end repeat




do shell script "/usr/bin/python <<'EOF' - " & args & "


# coding: utf-8


import sys, re


from Foundation import NSURL


from Quartz.PDFKit import PDFDocument




argv = [ a.decode('utf-8') for a in sys.argv[1:] ]


outdir = argv.pop(0).rstrip('/')


 


for f in [ a for a in argv if re.search(r'\\.pdf$', a, re.I) ]:


    url = NSURL.fileURLWithPath_(f)


    doc = PDFDocument.alloc().initWithURL_(url)


    path = doc.documentURL().path()


    pcnt = doc.pageCount()


  


    for i in range(0, pcnt):


        page = doc.pageAtIndex_(i)


        m = re.search(r'number: \\S*', page.string())


        if not m:


            print 'no matching string in page %d of %s' % (i + 1, path.encode('utf-8'))


            continue    # ignore this page


        name = m.group()


name = name.replace('number: ','')


        doc1 = PDFDocument.alloc().initWithData_(page.dataRepresentation()) # doc for this page


        if not doc1.writeToFile_('%s/%s.pdf' % (outdir, name)):


            print 'failed to save page %d of %s' % (i + 1, path.encode('utf-8'))


EOF"


end script


tell o to run


end _main

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Update AppleScript + Ruby script to work in High Sierra or Mojave

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