Stack windows on top of each other
I check in on the Nisus scripting forum every once in a while to see if there are any new tricks I can pick up for managing my online documents (such as my Gods & Monsters gamebooks).
On that forum, member bjast asked “How would I go about writing a macro to automatically stack all of the open windows exactly on top of each other?” Well, it turns out that Nisus macros are designed mainly for working within documents, and its window manipulation is not very extensive. But being a well-designed Mac OS X application, it also has AppleScript support. And there are standard tools for manipulating windows in AppleScript that most applications get automatically by supporting AppleScript.
[toggle code]
-
tell application "Nisus Writer Pro"
- copy windows to windowList
- copy the bounds of the first item of windowList to windowBounds
-
repeat with aLowerWindow in the rest of windowList
- if aLowerWindow is visible then set the bounds of aLowerWindow to windowBounds
- end repeat
- end tell
This will grab the “bounds” of the focus window and set all other windows in Nisus Writer Pro to have the same bounds. They’ll all go behind the focus window (which is almost always the top-most window).
This is useful for comparing similar documents. Besides Nisus Writer Pro, it also works in AppleScript Editor, AppleWorks 6, Mail, Microsoft Word X, Safari, Smultron, and TaskPaper1.
There appears to be some sort of oddity or bug in Terminal; it sometimes works in Terminal, and sometimes it positions lower windows horizontally but not vertically.
By only setting the bounds of windows that are “visible” the script ignores hidden windows, obviously, but also docked windows. It does not ignore windows in other spaces if you use Spaces; I couldn’t find a way to distinguish them. However, it will not pull them into the current space, it will just position them in their own space to be where your document is in the current space.
To use this, you need to enable the script menu in your Mac’s menu bar. When you go to AppleScript Editor to paste the script in, check your preferences to make sure that “Show Script menu in menu bar” is checked. Then, whenever you go into an application (such as Nisus) you can choose “Open Scripts Folder” from the script menu in your menu bar. Any script you place in the application’s folder will show in that menu when you use that application.
- August 1, 2012: AppleScript Preview in Snow Leopard and Lion
-
The one app that didn’t seem to support AppleScript control over window location was the one app it would have been most useful in: the built-in Preview application on Mac OS X.
It turns out, Preview does support AppleScript, but that support is turned off by default. Thanks to Trash Man at MacScripter.net for the commands that turn it back on:
- sudo defaults write /Applications/Preview.app/Contents/Info NSAppleScriptEnabled -bool true
- sudo chmod 644 /Applications/Preview.app/Contents/Info.plist
- sudo codesign -f -s - /Applications/Preview.app
The first line enables AppleScripting Preview; the next two lines are necessary in Snow Leopard, Lion, and, presumably, above, because starting with Snow Leopard Mac OS X signs the built-in applications. When you change the default, that changes the signature, and Preview will fail to start because its signature doesn’t match what the OS expects. The next two lines re-sign Preview so that the OS will accept it.
If you use Leopard or earlier, you should only need the first line.
Once you enable scripting, we can stack PDFs or other documents in Preview directly on top of each other for easy comparison using command-~ document switching.
[toggle code]
-
tell application "Preview"
- copy (every window whose visible is true and resizable is true) to windowList
- copy the bounds of the first item of windowList to windowBounds
-
repeat with aLowerWindow in the rest of windowList
- set the bounds of aLowerWindow to windowBounds
- end repeat
- end tell
Works great for comparing the differences between an original image and an optimized image, or for minor differences in the text of a PDF document.
Note that this is an improvement on the script in the earlier article. In the earlier script, if the first window happened to be an invisible or unmodifiable one, the script might end up resizing all of them to be too small to work with.
I’m testing this on Snow Leopard
↑
- Gods & Monsters
- Explore!
- Nisus
- I use Nisus Writer Pro for almost all of my new documents now. It’s a lot easier to use than the other word processors I’ve tried.
- Nisus Writer Pro Macros
- The official Nisus forum for discussing scripting Nisus, using the Nisus macro language or using more complex Perl blocks.
- Stacking Windows Macro at Nisus Writer Pro Macros
- “How would I go about writing a macro to automatically stack all of the open windows exactly on top of each other?”
More AppleScript
- Find all parent mailboxes in macOS Mail
- The macOS Mail app seems to want to hide the existence of mailboxes and any sense of hierarchical storage. These two AppleScripts will help you find the full path to a selected message and open the message’s mailbox.
- JXA and AppleScript compared via HyperCard
- How does JXA compare to the AppleScript solution for screenshotting every card in HyperCardPreview?
- Using version control with AppleScripts
- AppleScripts aren’t stored as text, which makes it impossible to track changes in AppleScript files using version control software such as Mercurial or Git.
- Save clipboard text to the current folder
- Use the Finder toolbar to save text on the current clipboard directly to a file in the folder that Finder window is displaying.
- Adding parenthetical asides to photograph titles on macOS
- Use Applescript to append a parenthetical to the titles of all selected photographs in Photos on macOS.
- 17 more pages with the topic AppleScript, and other related pages
More macOS tricks
- 42 Astoundingly Useful Scripts and Automations for the Macintosh
- MacOS uses Perl, Python, AppleScript, and Automator and you can write scripts in all of these. Build a talking alarm. Roll dice. Preflight your social media comments. Play music and create ASCII art. Get your retro on and bring your Macintosh into the world of tomorrow with 42 Astoundingly Useful Scripts and Automations for the Macintosh!
- Save clipboard text to the current folder
- Use the Finder toolbar to save text on the current clipboard directly to a file in the folder that Finder window is displaying.
- Avoiding lockFocus when drawing images in Swift on macOS
- Apple’s recommendation is to avoid lockFocus if you’re not creating images directly for the screen. Here are some examples from my own Swift scripts. You can use this to draw text into an image, and to resize images.
- What app keeps stealing focus?
- I’ve been having a problem on Mac OS X with something stealing focus. Here’s how to at least find out what that something is.
- Enable AirPrint for all connected Mac printers
- I have an iPad and an old workhorse of a printer, an HP 1012 LaserJet, connected to my iMac. I almost never need to print from the iPad, but when I do, handyPrint works.
- 14 more pages with the topic macOS tricks, and other related pages
More Nisus
- Importing an index into Nisus
- Nisus makes it very easy to import an externally-generated index into a document.
- Text to image filter for Smashwords conversions
- Smashwords has very strange requirements for ebooks. This script is what I use to convert books to .doc format for Smashwords, including converting tables to images.
- Nisus HTML script now handles floating content
- My Nisus simple HTML publish script now handles floating images and floating text boxes.
- Lulu, Nisus, and Gods & Monsters
- Lulu is sometimes really annoying. But they usually get the job done. Nisus, on the other hand, is rarely annoying to use and always gets the job done.
- Nisus Writer Pro 2.0
- The new Nisus is pure awesome: very easy to use, and it does everything I need.
- Four more pages with the topic Nisus, and other related pages
Given that the script is useful in a variety of applications, it would be useful to be able to place it in the Scripts folder and run it for any app; but while it will then appear in every application’s script menu, there’s no way of knowing what the current application is. “tell the current application” always tells Script Runner, not the application you called it from.