Apart from what @chrillek has said, You’re attempting to put rich text (RTF format) into a JavaScript variable. This would probably not produce the desired results. A better approach is to create a bookmark of the URL and convert it to RTF.
I tried it with an URL and just got a normal string with some special characters (but all just plain text). That, as well as putting RTF (which is just text, too) into a JavaScript variable doesn’t do any harm.
Alternatively, there is getTextFrom() which should get just the text from the HTML.
Yeah, it’s not going to produce an error. The OP would be disappointed though if they intend to preserve some formatting of the article using getRichTextOf().
If someone wants to process several records, the automation might be more convenient than leaving them one by one in the browser and saving the currents via the extension or services.
Another use case would be if you want to automatically retrieve contents of a specific website every day. So for example, you could set up an automation that on a daily basis, retrieves stock prices or current new stories or weather or whatever else might be of interest.
Moreover retrieving this as text rather than just bookmarking the website is helpful because you could extend the script to perform alert actions based on the content. For example, you could set it up to send yourself an email or a text anytime a significant event happened in the stock market or with the weather.
And that’s why I’m asking the reason for it. Even to this day, despite copious forms of documentation, there are people who don’t know about the services or browser extension.
I’m trying to send the text of many bookmarks to the openai api and make a summary, then save the api answer as markdown
It’s okay now, I can get the text and send it, but stay forever “Running” in Script Editor
Here is the full code:
(() => {
"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-projxxxxxx";
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 = `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);
let catalog = obj.choices[0].message.content;
let title = name;
let newSum = DTapp.createRecordWith({
name: title,
URL: aURL,
type: "markdown",
content: catalog,
thumbnail: thumb
});
});
})();
Test this line in a separate script file. It’s not going to turn up what you expect it would. You should call a method (e.g.r.name()) in order to retrieve information from r. Same for other lines.
It looks like obj.choices had produced undefined. That means the JSON data contained in the variable obj does not include the key choices. You might want to find out what actually is in the JSON for debugging purposes.
return objectName; is a simple way to show what is in objectName, which will be shown in the Result pane of Script Editor. You can return basically any object you’d like to inspect. MDN reference
This will work only if obj is text. The displayAlert method is for telling you that something has happened (when you run the script as e.g. a menubar script within the DT app), not exactly for debugging.
The error messages seem to indicate that the curl command was not properly quoted/escaped (an error message as text would be easier to read, btw).
This is the curl string in the script: