Execution error — AppleScript action to use TinyURL

I’m making an action to shorten URLs via TinyURL. I intend to use the action on Mac + iOS.

I’m mostly going to use this to shorten x-callback-urls so I can use them in Notion, but it’ll be useful in general I think.

I have this script, which works when I test it in the Mac Script Editor, and fails when I run it in Drafts.

Example script, works in Script Editor:

set draft to "bear://x-callback-url/open-note?id=DDF59ACC-6C42-42C6-B651-41D8B8B457CA-91312-0004D85C42EE3F2D&show_window=yes&new_window=yes"

set longUrl to draft
set shellScript to "curl https://tinyurl.com/api-create.php?url='" & longUrl & "'"
set shortenedUrl to do shell script shellScript
set the clipboard to shortenedUrl

Here is the same script, moved over to a Drafts AppleScript action:

-- AppleScript content
-- Editing and testing in Script Editor recommended
on execute(draft)
  set shellScript to "curl https://tinyurl.com/api-create.php?url='" & draft & "'"
  set shortenedUrl to do shell script shellScript
  set the clipboard to shortenedUrl
end execute

When I test the script in Script Editor, all good. When I try in Drafts, it fails with this error:

The operation couldn’t be completed. /Users/andrew/Library/Application Scripts/com.agiletortoise.Drafts-OSX/Temp/applescript-667161091.692247.scpt: execution error: Can’t make {permalink:"drafts://open?uuid=EA4D0705-01DA-486D-BDC5-B1E5201A76E7", folderName:"inbox", createdAt:date "Monday, February 21, 2022 at 9:53:53 AM", modifiedLongitude:-118.353204953042, accessedAt:date "Monday, February 21, 2022 at 10:20:07 AM", languageGrammar:"Markdown", modifiedLatitude:34.078812588259, content:"bear://x-callback-url/open-note?id=DDF59ACC-6C42-42C6-B651-41D8B8B457CA-91312-0004D85C42EE3F2D&show_window=yes&new_window=yes", modifiedAt:date "Monday, February 21, 2022 at 10:31:30 AM", createdLongitude:-118.353209668168, createdLatitude:34.078812275776, uuid:"EA4D0705-01DA-486D-BDC5-B1E5201A76E7", flagged:false, tags:{}, title:"bear://x-callback-url/open-note?id=DDF59ACC-6C42-42C6-B651-41D8B8B457CA-91312-0004D85C42EE3F2D&show_window=yes&new_window=yes"} into type Unicode text. (-1700)

draft is a draft object in the second script whereas draft is a string in the first script.

If you want the text contained in the draft object, try content of the draft.

Issues aside, AppleScript does not exist on iOS, so you may be barking up the wrong tree here if you say you want to use this cross-platform.

You would be better off using Drafts’ native HTTP script object to make the call the Tiny URL.

1 Like

Ah, good point. Hmm.

I was trying to do that but struggled to pass from JS to the clipboard. Is there a good example of that you can point me to?

There is an app.setClipboard function to set the contents of the system clipboard, if that’s what you needed.

Try this as a starting point. Not sure where you want to get your URL from…

let url = "drafts://open";
let endpoint = `https://tinyurl.com/api-create.php?url=${url}`;

let http = new HTTP();
let response = http.request({
	"url": endpoint,
	"method": "GET"
});

if (response.success) {
	app.setClipboard(response.responseText);
}
else {
	console.log(`${response.statusCode} ${response.error}`);
	context.fail();
}

That’s working well, thank you. I’m passing in the URL as the draft content.

So I’m getting it from the global variable, draft.content. Is there an easy way to strip any whitespace off front/end of passed in URL just in case I accidentally type an extra space or two on the end?! I’m now getting the URL from draft.content.trim() and it’s working. Thank you! I’ll share this to the action directory.

JavaScript has a built-in trim() function on strings. (ref)

Yep, just realized that, rusty on my JS. Thanks!

Shared to Action directory here, thank you very much for the help.

1 Like