Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

RGAlix

macrumors newbie
Original poster
May 27, 2022
4
0
Hey !

I'm trying to create a Quick Action on selected files (images & PDFs) that would :
1. resize images larger than 1200px
2. convert images to PDFs
3. combine all PDF documents (selected PDFs + images now converted to PDFs) into one PDF file
4. save this new PDF in the current folder (if possible with the name of the file on which the user right-clicked to launch the Quick Action)

I'm messing around with 'New PDF from Images' and 'Combine PDF Pages', but I can't seem to achieve what I want. I'm especially stuck when it comes to handling files and places (saving the document in the current folder, taking only images as input for an action, ...).

Could someone help me shed a light on this problem ?

Thank you very much,
Alix
 

yellow8

Suspended
Mar 14, 2017
540
1,061
Might I suggest the Textify app? It does all that thanks to Siri Shortcuts (if you run macOS Monterey), and it also makes the created PDF(s) searchable (thanks to OCR & text integration).
Please note that I'm the creator of this app.

Screenshot 2022-05-27 at 14.03.11.jpg

Screenshot 2022-05-27 at 14.02.11.jpg

Moreover, you can customize the shortcut to best fit your needs!
 
Last edited:

RGAlix

macrumors newbie
Original poster
May 27, 2022
4
0
Thanks for your reply. I would prefer being able to do it without another app, but I'll look into it. Can it work in the following scenario ?

1. I select small images (width < 1200px), large images and PDFs
2. I click an action and it will resize only the large images, leaving the small ones untouched
3. it creates a PDF out of all these files
 

yellow8

Suspended
Mar 14, 2017
540
1,061
For 2. and 3. I'm sure of it because there is a setting in Textify that allows resizing images to a max size. It won't resize the ones smaller than that.

For 1. this is more Shortcuts related. I strongly believe this might be possible to filter files, but I haven't done this yet.
 

kryten2

macrumors 65816
Mar 17, 2012
1,115
99
Belgium
Might not be exactly what you want but perhaps it's a start :

Automator :
  • Set Value of Variable : Storage
  • Filter Finder Items : Kind is image
  • Run AppleScript :
AppleScript:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
use framework "AppKit"

on run {input, parameters}
    repeat with aFile in input
        set inNSURL to (current application's class "NSURL"'s fileURLWithPath:(POSIX path of aFile))
        set theImage to (current application's NSImage's alloc()'s initWithContentsOfURL:inNSURL)
        set theSize to theImage's |size|()
        set theWidth to width of theSize
        if theWidth > 1200 then
            scaleImage(aFile)
        end if
    end repeat
    return input
end run

on scaleImage(anImage)
    -- your code goes here
    tell application "Image Events"
        launch
        set theImage to open anImage
        scale theImage to size 1200
        save theImage
        close theImage
    end tell
end scaleImage
  • New PDF from Images
  • Set Value of Variable : New PDF
  • Get Value of Variable : Storage !!Ignore Input!!
  • Filter Finder Items : Kind is PDF
  • Get Value of Variable : New PDF
  • Combine PDF Pages
  • Copy Finder Items
  • Rename Finder Items: Name Single Item

Screenshot 2022-06-01 at 10.46.15.png

Screenshot 2022-06-01 at 10.47.26.png

 

RGAlix

macrumors newbie
Original poster
May 27, 2022
4
0
Thanks a llot for your help, kryten2 !

I have an error when running the quick action :
The action “Combine PDF Pages” encountered an error: “The operation couldn’t be completed. Command line tool returned error 127.: 127”

I think I have something different from you in the location of New PDF from Images action (mine is Desktop, yours seems to be the current folder).

Any way you could share the workflow file ?

Thank you again !
Alix

P.S. : you seem to be quite more knowledgeable than me about Automator. Do you have any documentation on Automator to point me to ?
 

kryten2

macrumors 65816
Mar 17, 2012
1,115
99
Belgium
I have an error when running the quick action :
The action “Combine PDF Pages” encountered an error: “The operation couldn’t be completed. Command line tool returned error 127.: 127”
Probable cause here
I think I have something different from you in the location of New PDF from Images action (mine is Desktop, yours seems to be the current folder).
That's ok.

Replace last 3 actions with :

Run AppleScript :
AppleScript:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "Quartz"
use scripting additions

on run {input, parameters}
    
    (* Your script goes here *)
    set destPosixPath to POSIX path of (choose file name default name "Combined.pdf" with prompt "Save new PDF to:")
    its combineFiles:input savingTo:destPosixPath
end run

on combineFiles:input savingTo:destPosixPath
    -- make URL of the first PDF
    set inNSURL to current application's class "NSURL"'s fileURLWithPath:(POSIX path of item 1 of input)
    -- make PDF document from the URL
    set theDoc to current application's PDFDocument's alloc()'s initWithURL:inNSURL
    -- loop through the rest
    set oldDocCount to theDoc's pageCount()
    set inFiles to rest of input
    repeat with aFile in inFiles
        -- make URL of the next PDF
        set inNSURL to (current application's class "NSURL"'s fileURLWithPath:(POSIX path of aFile))
        -- make PDF document from the URL
        set newDoc to (current application's PDFDocument's alloc()'s initWithURL:inNSURL)
        -- loop through, moving pages
        set newDocCount to newDoc's pageCount()
        repeat with i from 1 to newDocCount
            -- get page of old PDF
            set thePDFPage to (newDoc's pageAtIndex:(i - 1)) -- zero-based indexes
            -- insert the page
            (theDoc's insertPage:thePDFPage atIndex:oldDocCount)
            set oldDocCount to oldDocCount + 1
        end repeat
    end repeat
    set outNSURL to current application's class "NSURL"'s fileURLWithPath:destPosixPath
    -- save the new PDF
    (theDoc's writeToURL:outNSURL)
end combineFiles:savingTo:

Do you have any documentation on Automator to point me to ?
The docs are out there. Automator is dying/dead. Shortcuts is the future.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.