(() => {
"use strict"
const DTapp = Application("DEVONthink 3");
DTapp.includeStandardAdditions = true;
const app = Application.currentApplication();
app.includeStandardAdditions = true;
const userInput = "You are a researcher, make a summary with the first subtitle pointing out the 5 main points addressed, in the second subtitle explain the 5 main terms used, in the third subtitle explain 3 complex concepts in simple terms and in the fourth subtitle give an example in everyday life of how the subject can be applied. The text is as follows: ";
const my_api_key = "sk-XXXXXXX";
let selection = DTapp.selectedRecords();
selection.forEach(r => {
let aURL = r.url;
let urlObject = r.url()
let markup = DTapp.downloadMarkupFrom(urlObject);
let text = DTapp.getTextOf(markup);
let name = r.name();
let thumb = r.thumbnail();
let UUID = r.uuid();
let prompt = userInput + text;
let command = console.log(`curl https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${my_api_key}" \
-d '{"model": "gpt-4o-mini", "messages": [{"role": "assistant", "content": "You are a helpful assistant"}, {"role": "user", "content": "${prompt}"}]}'`);
let jsonObj = app.doShellScript(command);
let obj = JSON.parse(jsonObj);
return obj;
let catalog = obj.choices[0].message.content;
let title = name;
let newSum = DTapp.createRecordWith({
name: title,
URL: aURL,
type: "markdown",
content: catalog,
thumbnail: thumb
});
});
})();
I can’t debug that stuff. You must inspect (aka log) the different strings to see what’s going on. And no, assigning the result of console.log to a variable is not part of the picture.
But If it is not possible to display a JSON object yet I need to view the JSON object to know what its contents are, how does one create debugging code?
But we’re not talking about JSON objects here, since the curl call doesn’t (obviously) return an object – it returns a string.
The script uses, unfortunately, a variable jsonObj, which contains the JSON string. It then calls JSON.parse(jsonObj) to convert this string to an object. But the string itself can be printed as any string, using console.log().
After trying the curl command in Terminal and working, then I thought the problem was in the text obtained from getTextOf(). I found how to solve it in this topic
prompt = prompt.replace(/(?:\r\n|\r|\n)/g, ' ');
Now it works good. I just need to how put the main title of the note like let aTitle = '# ${aName} - Summary'. But I think I’ll do it with a regular expression
(() => {
"use strict"
const DTapp = Application("DEVONthink 3");
DTapp.includeStandardAdditions = true;
const app = Application.currentApplication();
app.includeStandardAdditions = true;
const userInput = "You are a researcher, make a summary with the first subtitle pointing out the 5 main points addressed, in the second subtitle explain the 5 main terms used, in the third subtitle explain 3 complex concepts in simple terms and in the fourth subtitle give an example in everyday life of how the subject can be applied. The text is as follows: ";
const my_api_key = "sk-proj-xxxxx";
let selection = DTapp.selectedRecords();
selection.forEach(r => {
let aURL = r.url();
let markup = DTapp.downloadMarkupFrom(aURL);
let text = DTapp.getTextOf(markup);
let aName = r.name();
let thumb = r.thumbnail();
let aUUID = r.uuid();
let prompt = userInput + text;
prompt = prompt.replace(/(?:\r\n|\r|\n)/g, ' ');
let command;
command = `curl https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${my_api_key}" \
-d '{
"model": "gpt-4o-mini",
"messages": [
{"role": "system", "content": "You are a helpful assistant"},
{"role": "user", "content": "${prompt}"}
]
}'`
let jsonObj = app.doShellScript(command);
let obj = JSON.parse(jsonObj);
let catalog;
if (obj && obj.choices && obj.choices.length > 0 && obj.choices[0].message) {
catalog = obj.choices[0].message.content;
}
if (catalog) {
let newSum = DTapp.createRecordWith({
name: aName,
URL: aURL,
type: "markdown",
content: catalog,
thumbnail: thumb
});
}
});
})();
Good that it works for you now. But I’d still like to know why replacing all line feeds and carriage returns with spaces in the text would make the shell command work. You’re using a template string, which should not be derailed by newlines etc.
But the downloaded text probably contains line feeds and is added to the prompt. In the end there’s still no JXA equivalent of AppleScript’s quoted form of.
All quoted form of does most of the time is adding single quotes around a string.
set str to "a " & linefeed & "b" & return & "c"
log quoted form of str
`(*'a
b
c'*)`
Which is not very difficult to do in JavaScript – just include your string in single quotes When the string contains a single quote, ("it's my house"), quoted form of creates two concatenated strings (`‘it’'‘s my house’). In that situation, simply enclosing the string in single quotes in JavaScript doesn’t suffice.
But here, the problem seemed to go away by replacing newlines and carriage returns with spaces. That’s not something quoted form of does (to my knowledge), either. And I still don’t understand why newlines/carriage returns would be a problem in a single quoted string.
Good idea! The GPT response generates a Markdown that also has a main title, so I think by changing the prompt I would eliminate the title generated by the GPT, I guess