Putting the display to sleep (⇧⌃⏏/shift+control+eject) in AppleScript - macos

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

Related

Clear Recent items in Preview Application of Mac OS X..?

Hello all,
Can anyone tell me how to clear the 'Recent items' list in Preview application of Mac OS X through code? Is there a terminal command that can do this?
Or is there any way to click on 'Clear Menu' in 'Open Recent' of the 'File' menu in Preview?
Alternatively, does someone know where Preview stores this information and how do to remove it?
Check out this image to see what I mean.
Ok, so none of the described methods on the websites I could find actually succeeded in getting rid of all the 'recent document' lists in all of my apps.
I feel the most common sense approach would be (for OSX 10.12 / Sierra):
First go to system settings > general and choose "none" in the recent file dropdown menu (my OSX is in another language so the exact terms might be different, but you should be able to see what I mean). / This will get rid of most, but not all recent-items in different apps.
Get familiar with Apple's 'defaults' command in terminal. Now, hunt for any lists you might still want to get rid off, e.g. none of the options mentioned in the answers already listed here helped to get rid of the recent-items list in Finders "GO" menu. I played around with the defaults command and found that: "write com.apple.finder "FXRecentFolders" '({})' && killall Finder" does the trick for me. Playing around I found similar solutions for many of the other apps that where still able to maintain a list of recent items.
Create a shell script containing the commands you found during step 2.
Schedule the script to be run automatically on a preset interval or action (e.g. log out). AND/OR create an alias in your shell's profile (or directly apply the script as a function inside it) so you can call it with a single command from your terminal. (for instance: I have created an alias to it, so when I now type "killrecent" in terminal, it empties all the recent-items lists I've been able to find.
Hope this will be of some help to others. Good luck!
open Preview ->- go to File ->- Open Recent ->- Clear Menu
You may try the approach outlined here, which is to run
defaults delete com.apple.Preview.LSSharedFileList RecentDocuments
in the Terminal (manually or through your app). However I tried this and it didn't work for me (OSX 10.11), since the Preview defaults file doesn't contain this entry.
You can use
defaults write com.apple.Preview NSRecentDocumentsLimit 0
which will hide all recently used items, but as soon as you set that number to anything greater than 0, they will show up again.
I also checked the ScriptingBridge Interface for Preview, but couldn't find anything useful. So unfortunately it looks like this is not possible.
with 10.11, there are at
~/Library/Application\ Support/com.apple.sharedfilelist/com.apple.LSSharedFileList.ApplicationRecentDocuments/com.apple.preview.sfl

List windows visible on the screen in OSX

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 :)

How to smoothly make changes to Snow Leopard via bash script

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.

Quicksilver Large Type

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

In OSX can I use a system call to pass a command to a running terminal in a new tab?

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

Resources