Reputation: 5767
I want to have a script to show and hide a VIM window with a specific file with a keyboard shortcut (will be done over BetterTouchTool). I have everything in the script working, expect the hiding of the window.
I tried to find the opposite of AXRaise
, but had no luck to find some documentation regarding this.
if application "MacVim" is running then
log "macvim running"
tell application "System Events"
log "tell app system events"
tell application process "MacVim"
log "- tell app process macvim"
if exists (first window whose name contains "journal") then
log "-- window contains journal"
# @todo have to toggle between visible and not visible
if (get frontmost) then
log "true: " & (get frontmost)
# How to hide the window ??
else
log "false: " & (get frontmost)
# set visible to true
# set frontmost to true
perform action "AXRaise" of (first window whose name contains "journal")
end if
else
log "- no window with journal"
do shell script "/Users/foobar/bin/jo > /dev/null 2>&1 &"
end if
end tell
end tell
else
log "no macvim running"
do shell script "/Users/foobar/bin/jo > /dev/null 2>&1 &"
end if
Upvotes: 0
Views: 2921
Reputation: 7555
In the context of basic vanilla AppleScript, here are two ways the window of an application can be hidden.
miniaturizable
property is true
, setting its miniaturized
property to: true
visible
property to: false
visible
property of a window to false
, it no longer appears in the list of open windows shown under the Window menu in its UI, and will remain that way until it's reset to true
, while the application is still running.Example AppleScript code:
To hide by minimizing:
tell application "MacVim" to ¬
set miniaturized of ¬
(first window whose name contains "journal") to true
To unhide when miniaturized
is set to: true
tell application "MacVim" to ¬
set miniaturized of ¬
(first window whose name contains "journal") to false
Or:
tell application "MacVim" to ¬
set visible of ¬
(first window whose name contains "journal") to true
To hide by setting visible
to: false
tell application "MacVim" to ¬
set visible of ¬
(first window whose name contains "journal") to false
To unhide by setting visible
to: true
tell application "MacVim" to ¬
set visible of ¬
(first window whose name contains "journal") to true
tell application "System Events" to ¬
tell application "MacVim" to ¬
set miniaturized of ¬
(first window whose name contains "journal") to true
Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5
, with the value of the delay set appropriately.
Upvotes: 2