Make link with AppleScript in AppleScript

I wanted to reverse engineer the “Open this Scriptlet in your Editor”. In Xojo it’s one line of code:

TextArea2.Text = “applescript://com.apple.scripteditor?action=new&script=” + EncodingToURLMBS(TextArea1.Text)

Then I had the idea to do the same in AppleScript. The script below works for simple scripts like getting the mailboxes for Mail. But if I use the script on itself I get an empty script as result:

use framework "Foundation"
use scripting additions

tell application "Finder"
	set theFile to choose file with prompt "Please select a text file:" of type {"public.text"}
	set theFile to theFile as string
	set FileContent to read file theFile as string
	set theScript to "applescript://com.apple.scripteditor?action=new&script=" & my urlEncode(FileContent)
	open location theScript
end tell

on urlEncode(input)
	tell current application's NSString to set rawUrl to stringWithString_(input)
	set theEncodedURL to rawUrl's stringByAddingPercentEscapesUsingEncoding:4 -- 4 is NSUTF8StringEncoding
	return theEncodedURL as Unicode text
end urlEncode

What am I doing wrong?

I think, you should remove telling to Finder first of all.

Also, file specification has problem when using with AsObjC frameworks. Use alias reference instead:


use framework "Foundation"
use scripting additions

set theAlias to choose file with prompt "Please select a text file:" of type {"public.text"}

set FileContent to read theAlias
set theScript to "applescript://com.apple.scripteditor?action=new&script=" & my urlEncode(FileContent)
open location theScript


on urlEncode(input)
	tell current application's NSString to set rawUrl to stringWithString_(input)
	set theEncodedURL to rawUrl's stringByAddingPercentEscapesUsingEncoding:4 -- 4 is NSUTF8StringEncoding
	return theEncodedURL as Unicode text
end urlEncode

Thanks! But again if I use the script on itself the result is an empty script:

Hi.

The stringByAddingPercentEscapesUsingEncoding: method is deprecated after Mac OS X 10.11. The method to use now is stringByAddingPercentEncodingWithAllowedCharacters: with an NSCharacterSet parameter. I haven’t had any luck with any of the obvious URL…AllowedCharacterSet presets, but allowing just alphanumeric characters seems to work — with this script on itself at least. I haven’t tested it with other code. An “allowed character”, by the way, is one that doesn’t need to be percent encoded.


use framework "Foundation"
use scripting additions

set theAlias to choose file with prompt "Please select a text file:" of type {"public.text"}

set FileContent to read theAlias
set theScript to "applescript://com.apple.scripteditor?action=new&script=" & my urlEncode(FileContent)
open location theScript


on urlEncode(input)
	set rawURL to current application's NSString's stringWithString:(input)
	set allowedCharacters to current application's NSCharacterSet's alphanumericCharacterSet()
	set theEncodedURL to rawURL's stringByAddingPercentEncodingWithAllowedCharacters:(allowedCharacters)
	return theEncodedURL as Unicode text
end urlEncode

Hi,
wondering what wud be the code to open url in terminal.app, instead of script editor.
This wud be great to run scripts from markdown links (eg from obsidian)
Tried “osascript://com.apple.Terminal?action=new&script” did not work
This works “ssh://192.168.1.1”
Suggestions plz
Cheers

@Nigel Garvey: thanks!

@One208: you are mixing the terminal command and the url handler.

applescript:// is the url handler like https: or mailto: . The Terminal doesn’t have a useful url handler.

Hi bwill
I do remember coming across one. I was naïve about these URL then. I have used applescript URL for along time.
Popclip has a function to convert selected text into various formats camelcase/ plain text etc. I modified it to generate markdown url and paste it into obsidian
It is much easier to tag and search and retrieve scripts when needed in obsidian, and it is as easy as clicking link → it opens in script editor
If you create a markdown link with “ssh://One208@192.168.1.1”, and click it, it opens a terminal window asking password for user One208
I am sure url for terminal exists or may be not
Cheers

You can check the url handlers of each app yourself. They are in the plist. Below is a screenshot of my own app which reacts only to max-scheduler:

I need to check why I have an empty url handler.

Quick Check

we have 3 call out URL, but how to make them useful to call a precompiled shellscript or a string to be converted into command ?
telnet
ssh
man page