2017-06-01 44 views
0

我試圖設置一個簡單的Apple JXA腳本來將着名的聳肩表情符號寫入屏幕(它模擬按鍵我猜)。我嘗試的每件事情都會產生這個「¯\ _(a)_ /¯」。我假設,因爲我正在使用按鍵來模擬按鍵,並且我沒有日文鍵盤,所以我一直都會這樣做。也許我應該將字符串複製到剪貼板?JXA keystroke無法使用特殊字符

這裏是我到目前爲止的代碼:

// Shrug Script 
// ¯\_(ツ)_/¯ 
// 
// contains special character: 
// ツ 
// KATAKANA LETTER TU 
// Unicode: U+30C4, UTF-8: E3 83 84 

// thought maybe I needed to decode it ¯\_(ツ)_/¯ 
function decode_utf8(s) { 
    return decodeURIComponent(escape(s)); 
} 

// this is the newer "Script" aka Javascript way 
var sysEvents = Application('System Events'); 

// this didn't work: 
// sysEvents.keystroke("¯\\_(ツ)_/¯"); 

// this doesn't work either 
sysEvents.keystroke("¯\\_("+ decode_utf8("\xE3\x83\x84") +")_/¯"); 

回答

0

確定,所以將它添加到我的剪貼板,並立即將其粘貼是輕鬆了許多。這不是我的問題的直接答案,但最終的結果是我想要的。

蘋果腳本版本:

# Shrug Script 
# ¯\_(ツ)_/¯ 
# 
# contains special character: 
# ツ 
# KATAKANA LETTER TU 
# Unicode: U+30C4, UTF-8: E3 83 84 

set the clipboard to "¯\\_(ツ)_/¯" 
tell application "System Events" to keystroke "v" using command down 

JXA版本:

var app = Application.currentApplication() 
    app.includeStandardAdditions = true 
var seApp = Application('System Events') 

// set current app's clipboard 
app.setTheClipboardTo("¯\\_(ツ)_/¯") 
// paste! 
seApp.keystroke('v', { using: 'command down' }); 
0

我會強烈建議服務這種任務,因爲他們可以直接操作在幾乎任何應用程序的文本;不需要像GUI腳本這樣的黑客工具。在Automator中創建一個新的服務非常容易,它將輸出一段文本,並且可以在完成時將其綁定到系統偏好設置中的鍵盤快捷鍵。這裏是你可以很容易地適應一個例子:

Creating an osx service to insert the current date and time

+0

我爲什麼不認爲這是我之前,但不知道做了一個服務。我也會試試這個。謝謝! – Francisc0