Jump to content

OpenActions — Granular control over opening apps


Recommended Posts

Posted (edited)

Not sure if it is related, but after running this file action. The script to get current URL from Safari :

 

tell application "Safari" to return URL of front document 

 

Started failing. I did restart my laptop and it works again though. 

Edited by nikivi
Posted
6 hours ago, deanishe said:

but what is that icon?

 

A treasure chest.

 

5 hours ago, nikivi said:

Not sure if it is related, but after running this file action. The script to get current URL from Safari :

 

tell application "Safari" to return URL of front document 

 

Started failing. I did restart my laptop and it works again though. 

 

If you have two of the same browser, AppleScript will call the last open one. Not sure how to solve that. Haven’t yet (if it exists at all) found a way to uniquely identify the one we want (presumably the one in the foreground).

Posted (edited)
8 hours ago, vitor said:

Not sure how to solve that

 

Compare PIDs. The lowest PID is the "original" app instance, the highest is the one you just created:

// Compare PIDs
function sortByPid(proc1, proc2) {
    var pid1 = proc1.unixId()
    var pid2 = proc2.unixId()
    if (pid1 < pid2) return -1
    if (pid2 < pid1) return 1
    return 0
}

// Return array of processes, sorted by PID
function namedProcesses(name) {
    var results = []
    var procs = Application('System Events').processes.whose({name: name})
    for (i=0; i<procs.length;i++) {
        results.push(procs[i])
    }
    results.sort(sortByPid)
    return results
}

function run() {
    var procs = namedProcesses('Safari')
    console.log('oldest proc (' + procs[0].unixId() + ')', procs[0])
    console.log('newest proc (' + procs[procs.length-1].unixId() + ')', procs[procs.length-1])
}

 

Edited by deanishe
Posted

I've been playing with this and found some weirdness: AppleScript calls go to the newer instance of the app, but JXA calls go to the older one:

#!/bin/bash
open -a 'Safari' 'https://www.google.com'
sleep 10  # give app and page time to load
osascript -l JavaScript -e "Application('Safari').windows[0].currentTab.name()"
# Google
osascript -e 'tell application "Safari" to return the name of the current tab of the first window as text'
# Google

# Open a new instance
open -n -a 'Safari' 'https://www.yahoo.com'
sleep 10  # give app and page time to load
osascript -l JavaScript -e "Application('Safari').windows[0].currentTab.name()"
# Google
osascript -e 'tell application "Safari" to return the name of the current tab of the first window as text'
# Yahoo

 

Posted

@deanishe In this case, what I’d need is the PID of the frontmost app and a way make the AppleScript/JXA I use act on that specific one.

 

Reason being simply getting the earliest/latest PID does not work for me, as I have a regular script (launchd) that calls Chrome headless. So at any point the Chrome I want might be the first or last called.

Posted
5 hours ago, deanishe said:

Ignore any processes with 0 windows?

 

That’s a good idea. Will look into that.

 

But that only solves the issue for my use case. If there’s a way to detect the frontmost app by process ID (there is) and then use that process id to tell AppleScript and JXA to run the actions (I’ve yet to find it), we’d solve every case.

Posted

I'm not sure I follow you. 

 

If you've got the frontmost process, you can script that object. 

 

What I haven't been able to figure out is how to get an Application object from a Process one. 

Posted
18 hours ago, deanishe said:

What I haven't been able to figure out is how to get an Application object from a Process one. 

 

That’s my problem as well.

 

18 hours ago, deanishe said:

If you've got the frontmost process, you can script that object.

 

I haven’t been able to do that. For example, for Chrome I can Application('Google Chrome').windows[0].activeTab.name(). But if I try to do it by detecting Chrome as the frontmost app (Application('System Events').applicationProcesses.where({ frontmost: true }).windows[0].activeTab.name() or variations), I always hit an error.

Posted

That's the same issue. System Events gives you Process objects, not Application ones, so you don't have access to the application's dictionary/methods :( 

Posted

Update.


Added OneUpdater. Unlikely it’ll ever be needed, but still useful in case. Made the Workflow so fast, I forgot about it.

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...