jerik
jerik

Reputation: 5767

How to hide a specific window in apple script

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.

toogle-journal.scpt

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

Answers (1)

user3439894
user3439894

Reputation: 7555

In the context of basic vanilla AppleScript, here are two ways the window of an application can be hidden.

  • If the miniaturizable property is true, setting its miniaturized property to: true
  • Set its visible property to: false
    • Note: When setting the 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.
    • Closing MacVim, normally, with a window still hidden in this manner should cause a Quit without saving? There are modified buffers, if you quit now all changes will be lost. Quit anyway? [Cancel] [Quit] dialog box.

Example AppleScript code:

  • If MacVim is the active frontmost application then:

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

  • If MacVim is not the active frontmost application, then perform any of the above under System Events, e.g.:
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

Related Questions