Sending to an osascript a file name utf problems

aps = """osascript« END
tell application "Terminal"
    activate
    delay 1 -- just to free mod keys
    tell application "System Events"
        keystroke "c" using {control down}
        keystroke ("cd '%s'" as Unicode text)
    end tell
end tell
END""" % p
        exit = os.system(aps)

When the string, %s, contains accents (utf8-bom?) it doesn’t work. I don’t know how to encode de string to send it to keystroke

Try as «class utf8» instead of as Unicode text.

I’m a little confused about the purpose here. If you are working within the Terminal already, can you not change directory from within the shell?

It doesn’t work either. The original is:

$ cd …DOCENCIA/FP-IS/Planificación y temarios/…'

and the keystroked:

$ cd …DOCENCIA/FP-IS/Planificacioaan y temarios/…'

What I do, and it is a real useful shortcut from SublimeText is to move to the directory the file I’m editing is but sending the command to the Terminal, and waiting for me there and for me to press the Enter key when everything looks fine

the whole plugin for SublimeText is:

import sublime, sublime_plugin, os

# Fails with accents in path string

class cdTerminalWithPathCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        #  need ~ be typed (keystroke below) as ~space
        p = os.path.dirname(self.view.file_name()).replace('~', '~ ')
        p = p.encode('utf-8').decode('mac_latin2')



        # sublime.set_clipboard(p)
        aps = """osascript<< END
tell application "Terminal"
    activate
    delay 1 -- just to free mod keys
    tell application "System Events"
        keystroke "c" using {control down}
        keystroke ("cd '%s'" as «class utf8»)
    end tell
end tell
END""" % p
        exit = os.system(aps)
        # sublime.status_message("Copied file path only: " + p)

    def is_enabled(self):
        return self.view.file_name() is not None

I call it with the next keyboard shortcut:

    ,{ "keys": ["super+ctrl+alt+p"], "command": "cd_terminal_with_path" }

but although the original string is correct UTF from SublimeText, AppleScript keystroke doesn’t work well with it. I don’t know if it has something to do with the UTF-8 bom enconding the File System uses

You can see the problem here:

$ osascript -e 'tell application "System Events" to keystroke "áéíóú"'
$ aaaaa

Check your Terminal.app’s setting first.

Screenshot 2024-04-05 at 20.03.09

Settings > Profiles > Advanced > Text encoding:

You have to set this to "Unicode ( UTF-8) "

So, you can copy and paste the file name string to Terminal.

Screenshot 2024-04-05 at 20.08.12

I don’t have a solution but all I know is that I can reproduce it in both Terminal as well as just running the AppleScript portion in Script Debugger. @Piyomaru’s tip doesn’t make a difference.

Just a shot in the dark but I wonder if using ASCII codes will help (although I tried briefly and it didn’t make a difference either):

How aboud this? Iwrote “paste”. Understand?

set the clipboard to "áéíóú"

tell application "Terminal" to activate
tell application "System Events"
	tell process "Terminal"
		tell window 1
			keystroke "v" using {command down} --Paste
		end tell
	end tell
end tell

1 Like

That helps. Thanks.

Have you looked at this thread?

Oh ok I see, yes pasting should work, great idea.

I’d never seen the thread. Pasting un-keystrokable character is very classical know-how for non-English speakers. We use it from classic Mac OS era.

The most important point is here…There is a lot of AppleScript runtime programs.
And they have different authority level.

If you execute my script by Script Editor, it will work.
If you execute my script by osascript in Terminal.app, it will not work.

Your code seems to exectute AppleScript from other language by using osascript.
So, your runtime program may not be able to execute my script.

I didn’t want to destroy my clipboard. I try not no, especially when the command has nothing to do with the clipboard and leaves there something useless as a side effect. But you are right, it seems impossible for AppleScript to keystroke UTF-8 chars :frowning:

My final plugin is:

import sublime, sublime_plugin, os

# Fails with accents in path string

class cdTerminalWithPathCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        #  need ~ be typed (keystroke below) as ~space
        p = os.path.dirname(self.view.file_name()).replace('~', '~ ')


        def is_ascii(s):
            return all(ord(c) < 128 for c in s)

        if is_ascii(p):
            # sublime.set_clipboard(p)
            aps = """osascript<< END
tell application "Terminal"
    activate
    delay 1 -- just to free mod keys
    tell application "System Events"
        keystroke "c" using {control down}
        keystroke "cd '%s'"
    end tell
end tell
END""" % p
        else:
            sublime.set_clipboard(p)
            aps = """osascript<< END
tell application "Terminal"
    activate
    delay 1 -- just to free mod keys
    tell application "System Events"
        keystroke "c" using {control down}
        keystroke "cd '"
        keystroke "v" using {command down}
        keystroke "'"
    end tell
end tell
END"""
            sublime.status_message("Non ASCIIs in path")

        exit = os.system(aps)
    def is_enabled(self):
        return self.view.file_name() is not None

You can always temporarily assign the contents of the clipboard to a variable. Then when you’re done with this, you can restore the clipboard as required — or just use the variable you had set.

You are right, but the problem is the clipboard is not always text, and we are in Python here!

The clipboard can hold a host of formats — basically everything under the sun. You can also set the clipboard from the shell with pbcopy. I wouldn’t be surprised if python had its own clipboard commands, as well.


Here is something else to try…

osascript -s s -e 'set xid to id of "áéíó" ' -e 'character id xid'
--> "áéíó"

The first command generates the following list:

--> {225, 233, 237, 243}

From within applescript, it would look like this:

set osa to "osascript -s s -e 'set xid to id of \"áéíó\" ' -e 'character id xid ' "
do shell script osa
--> "áéíó"
1 Like

Oh! Thanks!! that’s a nice patch!!! great!

oh yeah it’s character id NOT ASCII character I dug up earlier… i knew there was something else!

Ok, the point is now typesetting those character ids… I don’t know how to use keystroke with them

Is this what you mean?

set np to "DOCENCIA/FP-IS/Planificación y temarios/"
set np to POSIX path of (path to desktop as text) & np

set npid to id of np
set chid to quoted form of character id npid

tell application "Terminal"
	do script "cd " & chid & " ; pwd" in tab 1 of window 1
end tell

After you mentioned that, I played around with character id but couldn’t figure anything out, so I didn’t comment. The next day I was reading the osascript man page and re-read about the -s option, which I’d always glossed over in the past.

Meanwhile, I’d been looking at trying to get accented characters in csvs into excel and was using character id to look for hidden characters (of a BOM) and then this morning tried using character id with osascript, hoping it would circumvent the keystroke mechanism — the way the clipboard does. Funny sometimes how you get from point A to point B.

One of the nice things about ‘id/character id’ is that they can each produce and take a list.

Also, you can combine it into a single osascript command, like so:

osascript -s s -e 'character id (id of "áéíó") '
1 Like