Tuesday, May 11, 2021

#1 2021-04-24 05:18:08 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 707

PDF from Clipboard, if exists append

Here is ASObjC Script that create PDF from Clipboard.

If the file do not exists it will create a new PDF, if the PDF do exists it will append page.

The nice thing about this script its very easy to copy pages from Preview to build a new PDF.
Its also possible to run a shortcut on iPhone with image as inputs, copy (on iPhone) and execute
this script to build PDF on the Desktop. Any image copy on mobile device become a clipboard item
if iPhone and Mac computer share the same Network


I have test it and it looks to work, maybe there is a better approach.
I'm not sure if bestPasteboardType approach works, I copy a file from Finder and got Icon Image.

Applescript:

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

set theSavePath to POSIX path of (path to desktop) & "Clipboard.pdf"

set favType to {"com.adobe.pdf", "public.tiff"}

set pasteboard to current application's NSPasteboard's generalPasteboard()
set bestPasteboardType to pasteboard's availableTypeFromArray:favType
if bestPasteboardType is not missing value then
   set clipData to pasteboard's dataForType:bestPasteboardType
   if clipData is not missing value then
       set image to current application's NSImage's alloc()'s initWithPasteboard:pasteboard
       if image is not missing value then
           set page to current application's PDFPage's alloc()'s initWithImage:image
           try
               if FileExists(theSavePath) then
                   set pdfURL to current application's |NSURL|'s fileURLWithPath:theSavePath
                   set myFile to current application's PDFDocument's alloc()'s initWithURL:pdfURL
                   set pageNum to myFile's pageCount()
                   myFile's insertPage:page atIndex:pageNum
                   log "Clipboard data append"
               else
                   set pageData to page's dataRepresentation()
                   set myFile to current application's PDFDocument's alloc()'s initWithData:pageData
               end if
               myFile's writeToFile:theSavePath
               log "Clipboard data write to file..."
           end try
       end if
   end if
else
   log "No Clipboard data"
end if

on FileExists(theFile) -- (String) as Boolean
   tell application "System Events"
       if exists file theFile then
           return true
       else
           return false
       end if
   end tell
end FileExists

@KniazidisR, Thanks for your input.
This is properly the final ASObjC script from me, with @KniazidisR suggestions.

Applescript:


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

set theSavePath to POSIX path of (path to desktop) & "Clipboard.pdf"

my pasteboardForType:{"com.adobe.pdf", "public.tiff"} savePath:theSavePath

on pasteboardForType:tagetPasteboardType savePath:theSavePath
   set pasteboard to current application's NSPasteboard's generalPasteboard()
   set pasteboardType to pasteboard's availableTypeFromArray:tagetPasteboardType
   if pasteboardType is not missing value then
       set clipData to pasteboard's dataForType:pasteboardType
       set image to current application's NSImage's alloc()'s initWithPasteboard:pasteboard
       set page to current application's PDFPage's alloc()'s initWithImage:image
       if fileExists(theSavePath) then
           set pdfURL to current application's |NSURL|'s fileURLWithPath:theSavePath
           set pdfDoc to current application's PDFDocument's alloc()'s initWithURL:pdfURL
           pdfDoc's insertPage:page atIndex:(pdfDoc's pageCount())
           log "Clipboard data append"
       else
           set pageData to page's dataRepresentation()
           set pdfDoc to current application's PDFDocument's alloc()'s initWithData:pageData
       end if
       pdfDoc's writeToFile:theSavePath
       log "Clipboard data write to file..."
   else
       log "No Clipboard data"
   end if
end pasteboardForType:savePath:

on fileExists(theFile)
   tell application "System Events"
       if exists file theFile then
           return true
       else
           return false
       end if
   end tell
end fileExists

Last edited by Fredrik71 (2021-04-26 11:52:00 am)


if you are the expert, who will you call if its not your imagination.

Online

 

#2 2021-04-26 12:41:04 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1797

Re: PDF from Clipboard, if exists append

Nice script, that might come in handy, Fredrik71.
I don't think it can be improved much. But it seems to me that one check of bestPasteboardType for missing value is enough, since the result of the other conditions depends only on the first one. Also, the handler for checking the existence of a file can be written more accurately:

Applescript:


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

set theSavePath to POSIX path of (path to desktop) & "Clipboard.pdf"

set pasteboard to current application's NSPasteboard's generalPasteboard()
set bestPasteboardType to pasteboard's availableTypeFromArray:{"com.adobe.pdf", "public.tiff"}
if bestPasteboardType is not missing value then
   set clipData to pasteboard's dataForType:bestPasteboardType
   set image to current application's NSImage's alloc()'s initWithPasteboard:pasteboard
   set page to current application's PDFPage's alloc()'s initWithImage:image
   if FileExists(theSavePath) then
       set pdfURL to current application's |NSURL|'s fileURLWithPath:theSavePath
       set myFile to current application's PDFDocument's alloc()'s initWithURL:pdfURL
       myFile's insertPage:page atIndex:(myFile's pageCount())
       log "Clipboard data append"
   else
       set pageData to page's dataRepresentation()
       set myFile to current application's PDFDocument's alloc()'s initWithData:pageData
   end if
   myFile's writeToFile:theSavePath
   log "Clipboard data write to file..."
else
   log "No Clipboard data"
end if

on FileExists(theFile) -- (input: Posix path, output: Boolean)
   tell application "System Events" to if exists file theFile then return true
   return false
end FileExists

Last edited by KniazidisR (2021-04-26 12:42:09 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)