Jump to content

[SOLVED] How to fix Text Encoding Error with AppleScript in Workflow

Recommended Posts

Here's what I'm trying to do:

I frequently capture information and store it in Ulysses for research on writing projects. I typically copy/paste it into Ulysses in the appropriate group. I created a workflow to automate this via Alfred. The workflow takes the contents of the clipboard and passes it to an Openurl to use Ulysses x-callback-url to add a new sheet. So far, so good. Frequently I copy/paste content that is online, and I want to keep the url of the content for later research. In order to automate this as much as possible, I used clipboard history. I copy the url, then copy the content, then insert it into Ulysses. In order to automate this completely, I modified my workflow so that it checks the chipboard history and if {clipboard:1} is a url, it combines that url with the content on the current clipboard and then passes it to Ulysses. I used an AppleScript to do this by storing the clipboard and previous item from clipboard history into a variable, loading those variables in AppleScript and then creating a new string.


Here's the problem: when I do this something is wrong with the encoding of the string and I get strange characters in it. For example, apostrophes become Äô. Text passed through the workflow fine until I added this AppleScript. I don't know if the problem is when the AppleScript loads the variables using system attribute or if the problem is when it returns the string. I've included the AppleScript below to show what is does. Does anyone know how to get the AppleScript to encode the text properly? I tried using Run NSApplescript instead. It seems to properly encode the text, but the problem is that I cannot find a way to pass the clipboard contents since it doesn't read workflow variables and as far as I know, can't access the clipboard history. If an NSApplescript action could access the clipboard that would be an ideal solution.


on run argv
	set theQuery to item 1 of argv
	set cbCurrent to (system attribute "cb_current")
	set cbPast to (system attribute "cb_past") 
	set httpPrefix to "http"
	set newQuery to theQuery

	if cbPast starts with httpPrefix then
		set newQuery to theQuery & "\n" & "%% Source: " & cbPast & "\n" & cbCurrent
		set newQuery to theQuery & "\n" & cbCurrent
	end if
	return newQuery

end run


Edited by vitor
Marked as solved
Link to comment

The simplest solution would be to leave AppleScript out of it. You're not doing anything that requires it. This shell script should do the same, but correctly:

if [[ "$cb_past" =~ http ]]; then
    echo "$query\n%% Source: $cb_past\n$cb_current"
    echo "$query\n$cb_current"


Link to comment

I tried a shell script first but I was running into problems with how it escaped markdown characters. Probably easy to fix, but to be honest it's been over a decade since I did any bash scripts so it wasn't obvious to me how to fix it.

I solved it by doing a python script. In the process I discovered that Alfred seems to always use the plain text version of the clipboard in workflows, so I had to use Python to pull the clipboard directly if I wanted html that had been copied instead of just text. Regardless, Python solved the problem.

Link to comment

I used NSPasteboard. For reference in case someone else lands here, here's what I used. The first option gets the html version of what's on the clipboard, the second version gets the text (how Alfred returns the clipboard in a workflow). In the first option, you could use other formats (rtf, etc) as per Apple's documentation.


	from AppKit import *
	# Get the current clipboard directly from Mac
	pb = NSPasteboard.generalPasteboard()

	# Check if we need html format from the clipboard
	if ulFormat == "html":
		cbDirect = pb.stringForType_("public.html")
		cbDirect = pb.stringForType_(NSStringPboardType)


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...