Jump to content

Save All Open Apps as Reusable Session


Recommended Posts

I realize a Mac will reopen all current apps for a user on restart. But what if you want to restart with a new app session and then restart later to a saved session. It's possible to have different users with different session sets for each one, so switching users at startup would do the same, but that seems an unnecessary complication. If it was possible to ask Alfred to i) save all the currently open apps as a workflow, ii) restart into a different workflow, and then iii) restart into the saved workflow, that would seem to be a more efficient use of resources. I am not aware of anything like this at the moment, quickly saving a session of all currently open apps, for some future launch. Any thoughts? Thanks.

Link to comment

Welcome @WritersCafe,

 

Moving this to Workflow Help, because it would be better served like that.


As far as I’m aware, there isn’t a user-facing way of saving session like it happens on shutdown / restart. That doesn’t simply save the open applications, but your state within them. Saving that to restore later might be too complicated (it would need to track changes that might be irreversible).


But if all you want to do is to save a list of open apps and reopen them at a later date, that’s simpler to do (though it still requires coding).

Link to comment
  • 6 months later...

Here's a JXA script that outputs a JSON list of all running apps in the Dock. The rest is fairly simple.

 

#!/usr/bin/osascript -l JavaScript

/**
 * Print list of macOS applications in Dock as JSON.
 */
ObjC.import('AppKit')

function run(argv) {
    let ws = $.NSWorkspace.sharedWorkspace
    // activationPolicy for apps in the Dock is regular
    // https://stackoverflow.com/a/20056414
    let apps = ObjC.unwrap(ws.runningApplications)
        .filter(app => app.activationPolicy == $.NSApplicationActivationPolicyRegular && !ObjC.unwrap(app.hidden))
        .map(app => {
            return {
                bundleId: ObjC.unwrap(app.bundleIdentifier),
                name: ObjC.unwrap(app.localizedName),
                path: ObjC.unwrap(app.bundleURL.fileSystemRepresentation),
                pid: ObjC.unwrap(app.processIdentifier),
            }
        })
        // ignore Finder
        .filter(app => app.name !== 'Finder')

    // sort apps by name, case-insensitive
    apps.sort((a, b) => {
        a = a.name.toLowerCase()
        b = b.name.toLowerCase()
        if (a < b) return -1
        if (a > b) return 1
        return 0
    })
    // apps.forEach(app => { console.log(app) })
    return JSON.stringify(apps, null, 2)
}

 

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