Everything I know about AppleScript I taught myself and was wondering if I missed any cool features. I know you can make the computer talk to and control applications but is there anything else it can do or is it time to move on to a new language?
The coolest thing about Applescript I've recently discovered, is that you can script almost anything on your mac. So even application, which don't support Applescript natively, can be used in a workflow.
This is possible, because you can just "press" buttons as if you're sitting on the computer.
tell application "GhostReader" to activate
tell application "System Events" to keystroke "n" using command down
I used this to copy and paste a website from Safari and have it read by GhostReader, a proprietary text to speech tool.
When it comes to Applescript, application control is where the action is. There's not much of a "wow" factor within Applescript itself unless you're a real language nerd. It's really more about presenting a set of easy-to-use tools to control the "wow" factor of other applications.
I've seen (and have) examples of Applescript playing simple card games and other text-based fun (well...as much fun as one can have viewing one display dialog after another), but these are (at best) academic exercises to show off the robustness of the language itself or a specific feature of Applescript.
Simple, but I use this all the time!
tell application "System Events"
display dialog "$msg" with icon stop buttons {"Foo", "Bar", "OK"} default button "OK"
end tell
Whenever I'm doing some shell programming, it's convenient for my operation to bring awareness into Finder, via a dialog.
Very handy.
You can automate everything on your Mac, this is a great time saver.
I remember coding shell on C++ on Windows, it's just a pain to automate Windows.
Related
TDLR
How to get something similar to the iTerm "dropdown hotkey/overlay" functionality to work with Kitty Terminal (on a Mac)?
I work on a Mac and used iTerm2 for a long time and integrated the "hotkey window" into my workflow. Since I've made the switch to Kitty I have been trying to get the same functionality but couldn't find something that suits my needs.
Caveat/Problems
The one app that has this built-in, that I know of, is also iTerm. There's one big difference with this implementation and the native iTerm implementation.
iTerm is more of a "drop-down", in that it functions as an overlay. The BTT implementation will literally show and hide the application. This means that whenever you are working with multiple desktops, and you trigger this shortcut, BTT will move you to the desktop where the application is.
A similar solution for Lunix is using tdrop. As far as I know there's no equivalent tool for MacOS
I find this quite annoying TBH and would love to know if anyone knows how to do the same thing, but in a "drop-down" or "overlay" fashion
What I've tried
BetterTouchTool (BTT)
This is the way I've set it up using BTT.
AppleScript
This does sort of the same thing, but without the use of BTT.
set appName to "kitty"
tell application "System Events"
if visible of application process appName is true then
set visible of application process appName to false
else
set visible of application process appName to true
end if
end tell
I want to open another separate application, open the projects/documents of that application in a iterative way and then close the application. I also want to close all the modal and non modals dialogs which popped up during the opening of the document. I want to close all the dialogs including the crash dialog in case the application fails/ crashes.
What will be the best way using cocoa or applescript to achieve this and from where i can get more detailed information?
If the app has a scripting interface, of course the best way is to do that.
You generally don't want to iterate in AppleScript, but rather to operate on all of the results of a query.
For example, for almost any application that implements the "standard suite", you can just:
tell app "TextEdit" to close windows
This is much simpler (and faster, and more likely to be implemented correctly in the target app) than:
tell app "TextEdit"
repeat with theWindow in windows
close theWindow
end repeat
end tell
Of course this may pop up save/abandon changes dialogs, and it may skip over or include dialogs and inspectors, and so on, depending on the application's user model.
More importantly, it won't work if the app doesn't support scripting (and the standard suite).
Also, it won't help at all with closing a crash report—that window is owned by CrashReporter, not the original application (which is a good thing, because you can't talk to the original application anymore, now that it's crashed…).
The alternative is the UI Scripting features in System Events. This will only work if assistive access is enabled. It can also be a bit fiddly to figure out which windows are the ones you want to deal with, and which controls are the ones you want.
For example:
tell app "System Events"
click button 1 of windows of application process "TextEdit"
end tell
This works by finding every window (no matter what kind) owned by the TextEdit process, and simulating a click on the first button in that window (the red close button).
If you google "AppleScript UI Scripting" you should find lots of different guides. The first hit I found was http://www.makeuseof.com/tag/applescripts-ui-scripting-mac/ and it looks like a decent place to start.
Is it possible to determine if application was launched from AppleScript or just by clicking the icon in Cocoa?
I would be very surprised if this actually was possible.
AppleScript is just a scripting language, meaning that its capabilities are somewhat limited as far as applications go.
So I'd say you're probably out of luck (sorry :( ).
I'm wondering how can I script applications which has no dictionary. All information that I've found tells me nothing. But my experience says me that there is a way.
For example:
tell application "Firefox"
return count of windows
end tell
Will work. And it will work with "Opera" and other applications without dictionary at all. So the questions are:
1) Why does it work?
2) What else work in such a manner? Is there a list of all such actions?
Thank in advance!
You can to some extent script applications without an applescript dictionary by using 'tell application "System Events"'
tell application "Keynote" activate
tell application "System Events" to keystroke "c" using {command down}
end tell
end tell
This example activates Keynote and then copies the current selection. You can use similar code in many applications even if they don't have an applescript dictionary provided you also have the "Enable access for assistive devices" option checked in the "Universal Access" System Preference.
Edit:
This document gives some details of how Cocoa provides some support for Applscript in all applications:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ScriptableCocoaApplications/SApps_about_apps/SAppsAboutApps.html
Under the heading Built-in Support for Standard and Text Suites
You can open the scripting dictionary of FireFox. Just open AppleScript Editor and go File → show dictionary, and choose FireFox from the list.
It just shows a very rudimentary dictionary. What happens is that the system library provides at least a basic set of commands (called the Standard Suite) to any Carbon or Cocoa app. This is what contains the definitions of window you used.
As Ian already wrote, in order to do more with such an app, you use the so-called UI scripting via System Events.
This nice website is a good place to start.
I am writing a program to sit in the background on osx 10.6, listen to keystrokes and record them, grouping them by window title. (No, I am not writing malicious software. I do not need this program to be sneaky in any way, I just want to have a safety net for when I have typed a huge email and then accidentally refresh the page (APPLE-R) instead of opening a new tab (APPLE-T)) I have already found apple's EventMonitorTest example for the keystroke capturing code, now I just need to find the "key window" title.
Does anyone know where I can find examples for this kind of functionality? Thank you!
A couple of possibilities:
You could use the Accessibility API (though of course keep in mind that 64-bit Carbon does not support this)
You could use the CGWindow functions introduced in Leopard
I suspect the first option will be easier to do this with, since the CGWindow API is somewhat low-level and treats all windows (application windows, menu bars, dock icons, etc.) more or less equally.