Is there a way in OSX to get a list of all applications that are visible on the screen? Can I get a list of all windows of each application, their sizes and position?
You can achieve that with a tool called wmctrl. It may not be installed on Mac OSX but you can get it with brew install homebrew/x11/wmctrl (first, get brew or any package manager that knows where to find wmctrl if you don't want to mess with git repos and errors).
Then, you should be able to get active windows with something like this:
BASH
wmctrl -l
You may utilize the output of that command into something else afterwards:
BASH
./myProgram $(wmctrl -l)
Hope this helps!
UPDATE:
In case your window manager isn't compatible with wmctrl, your best way out is to use AppleScript for that. There is pretty simple ways of doing it, like suggested in this answer. I think this approach will make it easier for you to get the window attributes.
AppleScript
tell application "System Events"
repeat with theProcess in processes
if not background only of theProcess then
[...]
Have fun and good luck :)
Related
I am using Ruby on a mac to open a dozen URLs one at a time with Nokogiri etc.
For each URL I need to let my ruby program know
whether to keep the URL window for further inspection or close it.
But I cannot see the terminal window and its prompt, it is hidden behind
the last URL window.
I have to click on the terminal window in order to bring it to the front, in order to enter my decision on the keyboard.
puts "close webpage?"
if gets =~ /^y/i then 1 ; else; 0; end;
I would like the terminal window to come to the front before it prompts
me for an answer.
I think the question is two fold
Is there a terminal command that tells a terminal window to become
the active one (the one in the front) that would work with mac iTerm.
The Apple script "bringiTermtofront" works in the applescript editor.
tell application "iTerm" to activate
Is there a way to execute a terminal command from ruby.
the ruby code
system "osascript bringiTermtofront.scpt"
brings the iTerm to the front.
For Question 1, one approach would be to write an Applescript to handle the switch, and then use the Terminal command osascript to run it from your Ruby code. You could also check if rb-appscript is still usable (it's no longer supported, but might work).
For Question 2, you have a few choices. Using backticks around the command will let you capture the output of a brief command, if you want to store the result of the command in a variable. (E.g. use grep or something similar).
The system method in Kernel is probably your best choice, though, as it will execute shell commands as if at the terminal.
Per the edit showing what script you're using, you need to execute the script as Applescript, not as a terminal script. You don't even need a separate file as it's just one line. This would be :
command = %q[osascript -e "tell application \"iTerm\" to activate"]
system(command)
You could also put the Applescript in a file and execute it using just
system("osascript bringiTermtofront")
See "Running shell commands from Ruby" for a little more help on how to interact with these methods.
Is it possible to write an AppleScript to put the display to sleep (which locks the display if the computer is set to lock on sleep)? You can do this from the keyboard by entering ⌃⇧⏏ (shift+control+eject); this leaves all the programs, etc., running, and just turns off the screen.
I've been wanting to do this for a while now. I just found out how in the man pages. You can use the following command to achieve instant display sleep. (I have tested it on OS X 10.10.)
pmset displaysleepnow
(no root privileges / sudo required!)
I'm not sure if this works for 10.9.4 yet but by all means give it a shot!
you can use: tell application "Finder" to sleep
Or use bettertouchtool. It is a small app which you can use native functions in addition to applescript.
do shell script "pmset displaysleepnow"
Edit 2015-08-23: This is possible (from the shell) as of OS X 10.9! Go see user3064009's answer for the update :-)
There's no good way to do this; there's a SuperUser question about this same thing. Depending on why you want this, however, there's one workaround I know: activate the screen saver. (This is what they suggest over on SuperUser). You can do this in AppleScript with launch application id "com.apple.ScreenSaver.Engine", or from the command line by running the application /System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine. I don't know whether or not this is technically documented anywhere, but it's worked for several iterations of the OS now. It may not do exactly what you want—your screen saver may, for instance, be colorful, which isn't helpful if you want a black screen—but it will be the same in that it does lock the screen if you have that set up.
For a nice catalog of other workarounds, check out this MacScripter thread: it documents that
There's no scriptable way to do this.
You can't tell AppleScript to key code EJECT, as there's no such key code.
You can use pmset to tell the display to go to sleep in one minute, but then you have to wait.
There's an undocumented IOKit way to do this; there's a mailing list post explaining how.
I haven't found an easy way to do it programmatically, but I did find a very small, free app that puts the display immediately to sleep, called "SleepDisplay." (There is another app of the same name that did NOT work for me.)
So you can just
tell application "SleepDisplay" to activate
Link: http://www.macupdate.com/app/mac/26234/sleep-display
I have a bash script that I run to toggle visibility for my desktop icons which is the following:
#!/bin/bash
DESKTOP=$(defaults read com.apple.finder "CreateDesktop")
if [ $DESKTOP == 1 ]
then
defaults write com.apple.finder CreateDesktop -bool false
else
defaults write com.apple.finder CreateDesktop -bool true
fi
killall Finder
it works, but there's 2 issues here I don't know how if it's possible to do using bash.
when running this script it It opens up terminal run the app and leave the terminal opened.
Is there any way to make this script run without have to open the terminal?
I've found this link Making an executable bash file run when clicked which use Platypus as a wrapper for your script, but I would like to know if it's possible to do something natively without any extra tool :)
when running the script the killall Finder makes everything restart so the screen blinks and you lose the actual state of your windows
Is is possible to make only the desktop icons fade-in/out smoothly without affecting windows and other resources based on Finder?
this Camouflage app works like this, so basically I would like to know if it's possible to replicate this behavior using bash script.
Well that's it, I hope it's easy to understand.
You could say that I could use this camouflage app for my needs but actually I'm trying to learn a bit about it and trying to do it myself (and obviously, with your help :)
Thanks in advance
Update
I was having a look at the second issue and I'm not sure but it seems that app in question doesn't hide the icons from destkop but instead, it creates an overlay between the most front level on the desktop(that is over the icons) and generate an image which is the same used as a desktop image. hence the fading effect, since it can control the image transition when appearing. what helped me analyze this was since every time you close the app the icons appear back. So it must have something do to with it.
If I am wrong the it's really possible to do other way, please share your opinion :)
Not sure about a solution for your second question, but for the first: take a look at the third answer for the StackOverflow question you linked to:
Making an executable bash file run when clicked
You can create an application bundle with AppleScript Editor or Automator that will run your script without opening a Terminal window.
You know how you can make Quicksilver display massive large type on your screen? (By Hitting . then typing free text, select View Large Type under actions and hit Enter).
Well, does anyone know of a way to do that programmatically? Also, is quicksilver even required or is it built into OS X? I would love to be able to trigger that from bash.
You can do this with Applescript, so therefore you can so it in bash with the osascript command:
osascript -e 'tell application "Quicksilver" to show large type "Hello, world."'
I think that "Large Type" was previously available in the services menu, but I don't see it in Snow Leopard. I might be wrong about that. A similar feature is found in Address Book -- right click on a telephone number.
There's no special sauce here. It's putting up a window with the text you type displayed in a large font. Duplicating the effect in a Cocoa app is trivial.
If you like to know how they exactly implemented it, you can look at the global method QSShowLargeType in their source here:
https://github.com/quicksilver/Quicksilver/blob/master/Quicksilver/Code-QuickStepInterface/QSLargeTypeDisplay.m
You could do it inside your browser with large-type.com:
$ open http://large-type.com/#YourText
It is confirmed to be fixed in the next release (after b56a7). Better keep an eye on the download area:
http://code.google.com/p/blacktree-alchemy/downloads/list
Specifically,
In OSX 10.6 from a system call, I want to open a file for editing with VIM in a preexisting terminal (i.e. Terminal.app) by opening a new tab.
Of course I can open a new instance of terminal
/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal -e vim MyFile
And, of course I can figure out the PID of the running instance of Terminal but I don't know how to pass a command into that running program. Or, if Terminal supports receiving commands and if it will open a new tab.
If someone knows how to do this with a similar system (e.g. linux and xterm) it could help me figure it out with OSX and Terminal - or, is there some other technique to prevent opening so many terminals instances?
EDIT: CHEAP SOLUTION
I created an AppleAcript script
on run app_arg
tell application "System Events"
tell application process "Terminal"
key code {55, 36}
set frontmost to true
key code {55, 17}
keystroke item 1 of app_arg
keystroke return
end tell
end tell
end run
and run it via the system call like so
/usr/bin/osascript NEWSCRIPT.scpt "args"
It's dirty but it gets the job done - thanks!
The way to accomplish the is with applescript. You can send applescript to things in OS X with the osascript command. I wasn't able to find anything quickly that directly shows how to open a new tab with a command running in it, but I was able to find a couple of references to automating Terminal.app in various other ways with applescript, and I think they may point you in the right direction.
Various random Terminal.app applescript hacks mostly centered around changing colors.
An applescript hack that opens a new terminal window without creating a new Terminal process.
A nice StackOverflow question about how to query an application to discover what applescript it supports (stolen from a comment on your question by #dmckee)
And this nice StackOverflow question concerning Terminal.app's applescript specifically (again stolen from a comment by #dmckee)
An even better exploration of the Leopard Terminal.app's applescript from Ruby no less
And from that last link, it looks like the only way to do it is to use applescript to send the Command-T keystroke to the terminal. That's ugly, but it'll work. And then you can send the command you want to execute. :-)
There are three ways to do this:
Use popen
Use system
Use exec family