Jump to content
vitor

OpenActions — Granular control over opening apps

Recommended Posts

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

Share this post


Link to post
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).

Share this post


Link to post
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

Share this post


Link to post

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

 

Share this post


Link to post

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

Share this post


Link to post
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.

Share this post


Link to post

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. 

Share this post


Link to post
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.

Share this post


Link to post

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

Share this post


Link to post

Update.


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

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...