Setting PDF data with JXA does not work

That’s a weird conversion (and IMHO a bug) applied by JXA, AppleScript doesn’t do that.

Just to clarify: if, for example, I have a jpeg image in DT, data would be the binary content of this image in AppleScript? And in JS I’d get some kind of hex ascii which should be equivalent to the binary data?
I’m asking because then it just might be possible to en/decode the stuff in JS. Perhaps.

Exactly.

DEVONthink will handle this soon, this works now over here:

app = Application("DEVONthink 3");
records = app.selectedRecords();
records.forEach(r => {
	resultRecord1 = app.createRecordWith({name:"Test", type:"PDF document", data:r.data()});
	resultRecord2 = app.createRecordWith({name:"Test", type:"PDF document"});
	resultRecord2.data = r.data();
});

@cgrunenberg that’s what I’m trying here, but gives me a Parameter error as described above. Do you mean you have this working in your internal builds for the next release?

Yes.

1 Like

Great. Another one added to my things-to-look-forward to in the next release.

Just curious if you’re willing to collaborate, why wasn’t it working? I always understood AppleScript → JavaScript was an automatic translation so there wasn’t much to do if it wasn’t working?

I almost when the route of using @houthakker’s script to evaluate AppleScript via Obj-C in a JSContext. Inception! :slight_smile:

It is but unfortunately it has a bunch of its on flaws & bugs (and doesn’t get much love from Apple). In the meantime you could of course use AppleScript which works flawlessly :rofl:

4 Likes

Will data behave like that across all types of records, eg images?

Of course.

:+1:

Criss, just to verify - this also means that something like resultRecord.data = window.pdf() ) (which is my original use case) should work now, right?

It should work, please post the complete script and I’ll try it.

A boiled down version of my script:

const app = Application("DEVONthink 3");
r = app.createRecordWith({name: "DEVONtechnologies", URL:"https://www.devontechnologies.com", type:"bookmark");
let rWindow = app.openWindowFor({record: r});	
app.createRecordWith({name: r.name(), URL:r.url(), type:"PDF document", data: rWindow.pdf()});

Thanks for testing Criss!

Basically working, the PDF is just a white page as the web page wasn’t loaded right after opening the window.

Not loaded yet, I guess? Like: it would be nice if we had a callback that fires when the page is loaded?

Maybe the create web/PDF document commands would be a better option instead of opening & capturing a window?

1 Like

You’re right, of course

Sorry @cgrunenberg, that delay actually is in my script, but I stripped it accidentally - this should work. Thanks for taking the time to test this!

const app = Application("DEVONthink 3");
r = app.createRecordWith({name: "DEVONtechnologies", URL:"https://www.devontechnologies.com", type:"bookmark");
let rWindow = app.openWindowFor({record: r});
while (rWindow.loading()) { delay(0.3); }
rData = rWindow.pdf()
delay(5);
app.createRecordWith({name: r.name(), URL:r.url(), type:"PDF document", data: rData});

That would definitely be the best, but the reason for this is that this works with logged in sites (e.g. paywalled sites) instead of create web/PDF document which holds no cookies. I’ve summarized pros and cons of different methods I tried in researching these workflows here: Wrong text (layer) when capture PDF from viewer window - #15 by mdbraber

Works fine.

1 Like