Can someone suggest how to start writing with ASobjC.
I know that was book from @Shane_Stanley but it is not available anymore.
Any suggestions, resources?
I want to access PDFKit from AS and don’t know how to start?
Piotr
Can someone suggest how to start writing with ASobjC.
I know that was book from @Shane_Stanley but it is not available anymore.
Any suggestions, resources?
I want to access PDFKit from AS and don’t know how to start?
Piotr
You need to understand the various classes within the PDFKit framework as implemented for Objective-C.
And other Objective-C frameworks such as Foundation, etc.
There are plenty of examples in MacScripter, past and present, that use PDFKit classes to solve PDF related issues, and these will be instructive for you.
Here is an example. Apple’s PDF viewer, Preview uses an obnoxious mango-colored yellow for its highlight annotation. Here is code that gets all highlight annotations and when it matches a known mango colored highlight, it replaces that highlight with a vivid yellow. As written, it simply writes the PDF to itself, though I have commented code that would apply a “_yl” filename suffix to an separate PDF.
-- Change_PDF_Highlight.applescript
-- Convert mango "yellow" highlight to pure yellow
use framework "Cocoa"
use framework "PDFKit"
use AppleScript version "2.7"
use scripting additions
property ca : current application
property mangoColor : {0.980392, 0.803922, 0.352941, 1.0}
property allAnnotations : {}
property HL : {"Highlight"}
set aPDF to POSIX path of (choose file of type "PDF") as text
(*
set outFile to (ca's NSString's stringWithString:aPDF)'s stringByDeletingPathExtension()
set outFile to outFile's stringByAppendingString:"_yl"
set outFile to outFile's stringByAppendingPathExtension:"pdf"
*)
set pdf to ca's PDFDocument's alloc()'s initWithURL:(ca's NSURL's fileURLWithPath:aPDF)
-- collect all Highlight annotations in the PDF
repeat with pageIndex from 0 to (pdf's pageCount()) - 1
set thisPage to (pdf's pageAtIndex:pageIndex)
repeat with anno in thisPage's annotations()
if (anno's type as text) is in HL then
copy anno to the end of allAnnotations
end if
end repeat
end repeat
-- compare Highlight colors to Apple's mango color and replace with yellow
repeat with anno in allAnnotations
set hlColor to anno's |color|
set rgbColor to (hlColor's colorUsingColorSpace:(ca's NSColorSpace's sRGBColorSpace()))
set r to rgbColor's redComponent()
set g to rgbColor's greenComponent()
set b to rgbColor's blueComponent()
set a to rgbColor's alphaComponent()
set currentColor to (ca's NSArray's arrayWithArray:{r, g, b, a})
if (currentColor's isEqualToArray:mangoColor) then
(anno's setColor:(ca's NSColor's yellowColor()))
end if
end repeat
-- pdf's writeToFile:outFile
-- update original PDF
pdf's writeToFile:aPDF
return
This AppleScript-Objective-C code was translated from a Swift example and was tested on macOS Sequioa and Tahoe.
Thank you for your response. I understand the classes from PDFKit, and even successfully applied some with JavaScript for Automation, because I found the rules how to map calls and properties from Objecive-C to JS. But inthe case of AppleScript I can’t find any „official” rules how to express methods and properties from ObjC to AS statements. I see in your example chain of 's (apostrophe-s) and assume that it is part of syntax, but is it everything what I should use?
Although there is minimal documentation such as the Mac Automation Scripting Guide section that talks about AppleScript and the Objective-C equivalent, a lot of this is picked up by observing example code.
Yes, once you have an ObjC object, you must use an apostrophe s with it when referencing its methods. You also have to fence ObjC items that conflict with identically named AppleScript items such as |string| or |length|() to name a couple.
My ability to write ASOC code has to do with several years of observing and adapting what others have done. Other than Shane’s book, you won’t find a great deal of reading material on how to effectively write ASOC code. And then there is that small matter of Apple constantly making changes and deprecations to the frameworks and classes that keep you on your toes.
ASOC does not provide toll-free bridging to all Objective-C frameworks and classes the way that JavaScript for Automation (JXA) provides, or PyOBJC. Though I have written JXA code, I am not a fan of it nor is it my goto solution now.