Jump to content

Calling External Trigger via Apple Script Steals Focus


Recommended Posts

When calling an external trigger via Apple Script (or via the URL alfred://), the focus on the current application is lost, even when the workflow doesn't contain any input elements. Is there any way to accomplish this so that alfred does its stuff in the background without stealing the focus from the frontmost app?

 

tell application id "com.runningwithcrayons.Alfred" to run trigger "name of trigger" in workflow "id of workflow"

Link to comment

I’m unable to reproduce. Could you share the workflow in question, as well as the method in which you’re calling it (not only the code, but also the way in which you’re invoking it, e.g. which app you’re using)?


Would also be useful to know the exact version of Alfred and macOS that you’re on.

Link to comment

I'm trying to build a chrono workflow that does scheduled tasks in the background. I was hoping to accomplish this in Alfred.

I'm using Alfed 5.5 on Sonoma 14.4.1

 

Here's the code for the LaunchAgent:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.jct.runalfredchrono.plist</string>
    <key>LimitLoadToSessionType</key>
    <string>Aqua</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/jct/Library/Application Scripts/alfredchronolauncher.sh</string>
        <string>periodic</string>
    </array>
    <key>StartInterval</key>
    <integer>60</integer>
    <key>KeepAlive</key>
    <true/>
    <key>RunAtLoad</key>
    <true/>
    <key>StandardErrorPath</key>
    <string>/dev/null</string>
    <key>StandardOutPath</key>
    <string>/dev/null</string>
</dict>
</plist>

 

And here's the code for the script called by the agent.

#!/bin/bash

open "alfred://runtrigger/jct.chrono/run/"

 

The LaunchAgent was loaded with:

launchctl load /Users/jct/Library/LaunchAgents/com.jct.runalfredchrono.plist

 

The workflow itself consists only of the external trigger so far (I'm planing to extend it as soon as the focus problem is solved).

Link to comment

You don’t need a separate script to run the trigger. And use the AppleScript instead of the URL:

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.jct.runalfredchrono.plist</string>
    <key>LimitLoadToSessionType</key>
    <string>Aqua</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/osascript</string>
        <string>-e</string>
        <string>tell application id &quot;com.runningwithcrayons.Alfred&quot; to run trigger &quot;run&quot; in workflow &quot;jct.chrono&quot;</string>
    </array>
    <key>StartInterval</key>
    <integer>60</integer>
    <key>KeepAlive</key>
    <true/>
    <key>RunAtLoad</key>
    <true/>
    <key>StandardErrorPath</key>
    <string>/dev/null</string>
    <key>StandardOutPath</key>
    <string>/dev/null</string>
</dict>
</plist>

 

Edited by vitor
Code correction
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...