Jump to content

Applescript reads Alfred Environment Variables in wrong encoding?

Recommended Posts

So I want to use Applescript (osascript Script Action) to read some variables from the workflow environment. While everything works smoothly, I encountered an issue when the variable included special characters, in my case the German Umlaute (ä ö ü). Via some testing, I could isolate the issue to this:

set original to "ä" --this string has also been saved as {var:test} in the Alfred Workflow Envrionment
set no_encod_specified to (system attribute "test")
set utf8 to (system attribute "test") as «class utf8»
set utf16 to (system attribute "test") as Unicode Text

return original & " " & no_encod_specified & " " &  utf8 & " " & utf16
--result: "ä aÃà aÃà aÃà"


Applescript itself handles the special characters correctly, Alfred by itself, too. Only in this case the text is not read properly. When googling for the problem a bit, I quickly ran into some explainers how applescript can read files properly as utf8 or utf16. Bus as displayed in my code example, it didnt help either. 


I assume I should not the first one to have this issue, but the only thread on it recommended to use sth different than applescript 😕 isn't there a good solution for this?

Edited by armenaton
Link to comment
9 hours ago, armenaton said:

I quickly ran into some explainers how applescript can read files properly as utf8 or utf16.


Yeah, but it can't read UTF8 ARGV or environment variables properly. It assumes everything is MacRoman encoding.


You can either add some shell scripting to your AS, or use a less awful language.


set _decoded to do shell script "echo " & quoted form of (system attribute "SOME_VARIABLE") & " | iconv -f UTF-8-MAC -t MACROMAN"


Link to comment

Thanks (again)!


13 hours ago, deanishe said:

or use a less awful language.

yeah^^ Well thing is, I am not really a coder, only had some computer science classes in school. Up until now; I mainly used Applescript to automate very small stuff, never wrote something with more than ~15 lines of code. Now this Pandoc workflow is the first bigger thing I am ever writing. It was fun, but as coding is neither my day job nor my side job, I don't whether I am ever going to code so much again. But if I do, I will strongly consider using a language other than Applescript. More than one time, I ran into some really unnecessary problems due to it. 



Edited by armenaton
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...