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
$ 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
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
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):
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
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
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.
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
--> "áéíó"
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: