vitor Posted June 21, 2017 Share Posted June 21, 2017 (edited) The open command gives us extra control when opening apps, such as making new instances or relegating to the background. This Workflow emulates those options with File Actions. Download | Source Edited November 22, 2020 by vitor GatorMapi, Andrew, nikivi and 1 other 4 Link to comment
deanishe Posted June 22, 2017 Share Posted June 22, 2017 Very nice, but what is that icon? Link to comment
nikivi Posted June 22, 2017 Share Posted June 22, 2017 (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 June 22, 2017 by nikivi Link to comment
vitor Posted June 22, 2017 Author Share Posted June 22, 2017 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). Link to comment
deanishe Posted June 23, 2017 Share Posted June 23, 2017 (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 June 23, 2017 by deanishe Link to comment
deanishe Posted June 23, 2017 Share Posted June 23, 2017 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 Link to comment
vitor Posted June 23, 2017 Author Share Posted June 23, 2017 @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. Link to comment
deanishe Posted June 23, 2017 Share Posted June 23, 2017 Ignore any processes with 0 windows? Link to comment
vitor Posted June 23, 2017 Author Share Posted June 23, 2017 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. Link to comment
deanishe Posted June 23, 2017 Share Posted June 23, 2017 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. Link to comment
vitor Posted June 24, 2017 Author Share Posted June 24, 2017 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. Link to comment
deanishe Posted June 24, 2017 Share Posted June 24, 2017 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 Link to comment
vitor Posted June 26, 2017 Author Share Posted June 26, 2017 Update. Added OneUpdater. Unlikely it’ll ever be needed, but still useful in case. Made the Workflow so fast, I forgot about it. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now