Skip to content

Instantly share code, notes, and snippets.

@malicious
Created February 12, 2024 15:54
Show Gist options
  • Save malicious/e5ffbf7f47f45c031f1072c5aed66f76 to your computer and use it in GitHub Desktop.
Save malicious/e5ffbf7f47f45c031f1072c5aed66f76 to your computer and use it in GitHub Desktop.
AppleScript to print current window/document, intended to show on MacBook Pro touchbar via MTMR.app
global frontApp, frontAppName, windowTitle
set resultString to "[no app]"
set frontAppName to ""
set windowTitle to ""
tell application "System Events"
try
set frontApp to first application process whose frontmost is true
set frontAppName to name of frontApp
set resultString to frontAppName
if {frontAppName is equal to "Electron"} then
set resultString to "VS Code"
end if
end try
try
tell process frontAppName
tell (1st window whose value of attribute "AXMain" is true)
set windowTitle to value of attribute "AXTitle"
-- set resultString to frontAppName & " - " & windowTitle
end tell
end tell
end try
end tell
return resultString
global frontApp, frontAppName
global windowTitle, windowContent
set resultString to "[no info]"
set windowTitle to ""
set documentPath to ""
-- set a delay so we can switch out of AppleScript Editor
-- delay 2
tell application "System Events"
set frontApp to first application process whose frontmost is true
set frontAppName to name of frontApp
tell process frontAppName
-- read the active window
try
tell (1st window whose value of attribute "AXMain" is true)
if exists (value of attribute "AXTitle") then
set windowTitle to value of attribute "AXTitle"
set resultString to windowTitle
end if
if exists (value of attribute "AXDocument") then
set documentPath to value of attribute "AXDocument"
set resultString to documentPath
end if
end tell
-- trim the leading prefix, maybe
if {resultString starts with "file:///"} then
set resultString to text 8 thru -1 of resultString
end if
if {resultString starts with "/Users/user/"} then
set resultString to "~/" & (text 13 thru -1 of resultString)
end if
if {resultString starts with "/System/Volumes/Data"} then
set resultString to text 21 thru -1 of resultString
end if
-- app-specific
if (frontAppName is equal to "Atom") then
if {resultString starts with "/~/"} then
set resultString to text 2 thru -1 of resultString
end if
end if
end try
-- read end/final of open file
try
tell (1st window whose value of attribute "AXMain" is true)
-- retrieve the document path, maybe
if exists (value of attribute "AXDocument") then
set documentPath to value of attribute "AXDocument"
end if
-- trim the leading prefix, maybe
if {documentPath starts with "file:///"} then
set documentPath to text 8 thru -1 of documentPath
end if
end tell
end try
end tell
end tell
-- read the last line of the given file
try
-- don't use applescript delimiting, because it dies on longer files, for some reason.
-- set resultString to last item of (read documentPath using delimiter linefeed)
-- don't use applescript at all, because UTF-8 is unworkable
-- set resultString to text -72 thru -1 of (read documentPath)
-- set resultString to text -72 thru -1 of (read documentPath as class "«utf-8»")
-- set resultString to (resultString as Unicode text)
-- eh, just do this instead
--set resultString to (do shell script "echo \"" & resultString & "\" | pbcopy")
--set resultString to (do shell script "echo \"" & resultString & "\" | iconv -f UTF-8-MAC -t MACROMAN")
-- just cut, painfully. need sed and tr to do multi-character replacement
set documentWords to (do shell script "wc -w \"" & documentPath & "\" | awk '{print $1}'")
set documentContent to (do shell script "tail --bytes=70 \"" & documentPath & "\" | sed 's/$/\\\\n/' | tr '
' ' '")
if {documentContent is not equal to ""} then
set resultString to documentWords & "字 | " & documentContent
end if
end try
return resultString
[
/*
{
"type": "group",
"align": "left",
"title": "應用程式",
"width": 90,
"width-comment": "big enough for the text or N close buttons",
"items": [
{"type": "close"},
{"type": "close"},
{"type": "close"},
{"type": "dock", "autoResize": true},
{"type": "network", "width": 180, "bordered": false, "align": "right"}
]
},
*/
{
"type": "appleScriptTitledButton",
"refreshInterval": 2.0,
"source": {
"filePath": "~/Library/Application Support/MTMR/app-info.scpt"
},
"textAlign": "left",
"width": 100
},
{
"type": "appleScriptTitledButton",
"refreshInterval": 2.0,
"source": {
"filePath": "~/Library/Application Support/MTMR/document-info.scpt"
}
},
/* {"type": "inputsource", "align": "right"}, */
{
"type": "battery",
"align": "right",
"bordered": false
},
{
"type": "shellScriptTitledButton",
"width": 80,
"refreshInterval": 5,
"source": {
"inline": "echo $(ps axo %cpu | awk '{s+=$1}END{print s}')'%'"
},
"align": "right",
"bordered": false
},
{
"type": "timeButton",
"formatTemplate": "YYYY-MM-dd HH:mm",
"align": "right",
"bordered": false,
"longAction": "shellScript",
"longExecutablePath": "/usr/bin/pmset",
"longShellArguments": ["sleepnow"]
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment