How to Get Metadata from a Document in "Preview" into KM?

I'd like to get the data in the "Creation date" field from the application "Preview" (red rectangle)
See this screenshot, which is also an export from UI browser. So, you can see how that program sees the window.

It is a floating window, i think KM has a tough job with that. Also applescript fails here, at least my tries.
Yes, i can do a Image Search, that works, but, we like to avoid that don't we? :slight_smile:

Please note that the creation date of the PDF (or the png or the jpg) can be different from the creation date of the file.

But according to your screenshot, I guess, you want the creation date of the PDF (in this case), not the one of the file.

Usually you can retrieve these data from the exif data (not for PDFs), and if not present still from the data of the image (not the file).

So, some questions:

  • Do you have only PDF images? (as in your screenshot)
  • if Not, what else?
  • Would it be fine for you to treat the file creation date the same as the image creation date?

Hey @Fokke,

Well, that's a bit of a sticky wicket...

This works on macOS 10.12.6 (Sierra) to pull all the data out of the window for later parsing:

------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2019/07/31 19:52
# dMod: 2019/07/31 19:52 
# Appl: Preview, System Events
# Task: Extract Information from Floating General Info Window.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Preview, @System_Events, @Extract, @Information, @Floating, @General, @Info, @Window
------------------------------------------------------------

tell application "System Events"
   tell application process "Preview"
      tell (first window whose subrole is "AXSystemFloatingWindow")
         
         tell radio group 1
            tell radio button 1
               perform action "AXPress"
            end tell
         end tell
         
         set dataRec to value of every static text
         
      end tell
   end tell
end tell

set dataRec to items 1 thru -2 of dataRec

set newDataRec to {}

repeat with fieldNum from 1 to (length of dataRec) / 2
   set end of newDataRec to (item fieldNum of dataRec) & space & space & (item (fieldNum + 13) of dataRec)
end repeat

set AppleScript's text item delimiters to linefeed

set newDataRec to newDataRec as text

------------------------------------------------------------

The window structure may be different in later versions of the macOS.

-Chris

Hi Tom, Yes, i am aware of the difference between the creation date of the file versus the image(pdf, png,jpg etc)

The files i want to use it from are 80% pdf's. Some are png'sor jpg's but then the creation date may be false and has to be user edited anyway.

Hey Tom,

Yeah, that works!
Although i had to add
activate application "Preview"
As the floating window only appears when the application is at front.
and, i have to open it, if it is not there already. But that is easy in KM.
Thanks!

Tom???  :wink:

-Chris

Hey @Fokke,

I would think you'd be running this macro from the Preview context, so it would already be activated.

And yes – the script was to only scrape the data from the get-info window – all other macro actions are incumbent upon the user.

-Chris

Oeps!

Sorry! :slight_smile:

1 Like

I really like your idiomatic expressions. Will make a Quiver folder to collect them :wink:

1 Like

I can help you get the creation date, along with other PDF attributes, without having to involve Preview. I am operating out of High Sierra, so cannot test this on later versions of macOS myself, but hopefully the AppleScript below will do something useful:

use framework "Foundation"
use framework "Quartz"

property this : a reference to current application
property nil : a reference to missing value

property NSString : a reference to NSString of this
property NSURL : a reference to NSURL of this
property PDFDocument : a reference to PDFDocument of this

on fileURLWithPath:(filepath as text)
	local filepath
	
	NSURL's fileURLWithPath:((NSString's ¬
		stringWithString:filepath)'s ¬
		stringByStandardizingPath())
end fileURLWithPath:


set PDFFilePath to "~/Google Drive/Scripting/os x and ios internals.pdf"

tell PDFDocument's alloc()
	initWithURL_(my fileURLWithPath:PDFFilePath)
	set PDFAttributes to the documentAttributes() as record
end tell

return the CreationDate in the PDFAttributes

This returns an AppleScript date object, which are easy to manipulate according to your needs. To give you an example of what documentAttributes() returns, for the particular PDF that I used, the resulting record was (formatted over several lines for easier reading):

{CreationDate:date "Tuesday, 16 October 2012 at 03h07m30", 
Producer:"Acrobat Distiller 9.5.2 (Windows)", 
ModDate:date "Wednesday, 5 December 2012 at 13h24m34", 
Title:"Mac OS® X and iOS Internals", 
Author:"Jonathan Levin", Creator:"Adobe Acrobat 7.0"}

As you know, different PDF's will have different attributes, and none of them are mandatary, I believe. You can retrieve the list of attributes for a PDF document by accessing documentAttributes()'s allKeys() as list, which, for the above, would return a list like this:

{"CreationDate", "Producer", "ModDate", "Title", "Author", "Creator"}

If you need any more specific help with implementing any of this, feel free to say.

4 Likes

Fantastic! It works!

1 Like