1

It it possible to detect application focus event using applescript? What I'm trying to achieve is to open ios simulator when I focus on text editor. I know how to activate simulator but can't figure out how to detect when the editor is active.

  • 1
    What version of OS X/macOS and Xcode? – user3439894 Jul 26 '17 at 17:55
  • @user3439894 macOS 10.12, xcode 8.3.3 – Levani Jul 26 '17 at 18:05
  • Simulator is not AppleScript scriptable, meaning it does not have an AppleScript Dictionary. That leaves you with AppleScript UI Scripting and some manipulation using System Events in AppleScript, both of which are rather limited compared to an app that actually supports AppleScript via an application specific AppleScript Dictionary. – user3439894 Jul 26 '17 at 21:39
  • A simple "on idle handler" is all that's needed for your desired feedback. I do not even see what scriptability of Simulator could help you. Just let a background script run on your Mac like I describe below … – clemsam lang Nov 29 '18 at 17:06
1

A simple solution, using AppleScripts immanent capacities

[I am a little ashamed to admit that due to an "unhappy localisation" I did not realise there is a perfectly suitable way to make AS-scripts repeat periodically. The English term "Stay Open Application" in its German version ominously re-translates to "Display after run-handler". ]
(I will leave parts of my workaround solution at the bottom, because of its own merit.)

This …

on idle
tell application "System Events"
    if frontmost of process "TextEdit" is true then ¬
        tell application "name_of_simulator" to activate
    return 2  -- this number tells System at what intervals to execute
end tell
end idle

… is really all you need.
Save this code in ScriptEditor, check "Stay Open" application (or similar) and run the app.
(Close ScriptEditor if you like.)


"Working solution" to detect change of frontmost application
[ … ]     AppleScript's problem is the "timing"…           <==         WRONG !!!

In order to display my current workspace's index in the right-side menu bar (as in older OSX versions: [2]) I use –and recommend!– Mat Ryer's app "BitBar" that lets you place any script in its plugins folder. Their names, e.g. "workspace.2s.sh", cleverly determine the interval between executions of the script: "2s" denoting every two seconds (minutes, hours, days…).

#!/bin/bash _/¯ osascript /Users/userName/.config/bitbar/Workspace.app calls a script that compares my desktop picture to a stored list and so returns my workspace number.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .