Jump to content

Help with getting output from AppleScript to JSON?


Recommended Posts

What was suggested to me in another topic is that I need to use "JXE" and Script Filter JSON Format

But I'm not familiar with JXE :( 

I have an AppleScript to pull a list of playlists from the active Apple Music app

tell application "Music"
  get name of playlists
end tell

 

How would I put this into "JXE" so it can return JSON? to using in a Script Filter?

Edited by Smpl
Link to comment
  • Smpl changed the title to Help with getting output from AppleScript to JSON?
var music = Application('Music');
music.activate();

music.currentPlaylist.name();

 

I've been trying to learn a bit here; Stating from scratch basically. 

 

I'm able to get the name of the playlist that is currently playing in Apple Music, but have yet to figure out how to get a list of all playlists like you can with the AppleScript in the  first post.

There are 3 options userPlaylist, LibraryPlaylist, SubscriptionPlaylist oh and FolderPlaylist but I haven't figure out how to use those yet. :(

 

Pressing onward..

Link to comment
var music = Application('Music');
music.activate();

music.playlists.name();

 

well derp; looks like I figured it out. I swear I tried this hours ago and it didn't work 🤦🏼‍♂️

 

Now I have absolutely no clue about JSON

 

["Library", "Music", "Music Videos", "My Top Rated", "Recently Played", "Alfred Playlist", "Breakup Songs", "Cruise Control", "Electronica", "Folk Music", "Gaming", "Hip Hop", "Kuntry", "My Shared List", "Megans List", "Private Songs", "Work Out", "YouTube Sync"]

 

This is what is outputted in the Results of Script Editor using the JXA (JavaScript) code above.

 

Could really use the help here on how to convert this list to a JSON, or at least point me to some documentation on it and I'll try to figure out. But I've never used JSON before.

Link to comment
16 hours ago, Smpl said:

But I'm not familiar with JXE :( 

 

It's "JXA", not "JXE". JavaScript for Automation.

 

3 hours ago, Smpl said:

Could really use the help here on how to convert this list to a JSON

 

To return JSON from a JXA script, you usually want return JSON.stringify(myResultsObject) in your run function.

 

Link to comment
2 hours ago, deanishe said:

 

It's "JXA", not "JXE". JavaScript for Automation.

 

 

It was obviously a typo; as I typed it correctly in the next post.

 

Quote

To return JSON from a JXA script, you usually want return JSON.stringify(myResultsObject) in your run function.

 

thanks, I'll try to figure out how to make that work.

Link to comment

This may be what you want:

 

const music = Application("Music");

const playlistItems = music.playlists.name().map(playlist => {
  return {
    "uid": playlist,
    "title": playlist,
    "arg": playlist
  }
})

JSON.stringify({ "items": playlistItems })

 

Link to comment
6 hours ago, vitor said:

This may be what you want:

 

const music = Application("Music");

const playlistItems = music.playlists.name().map(playlist => {
  return {
    "uid": playlist,
    "title": playlist,
    "arg": playlist
  }
})

JSON.stringify({ "items": playlistItems })

 

 

Thanks, this works pretty well! However it only lets me select one of the 'results' from the script filter list either by pressing enter on it or the ⌘{number} keyboard shortcuts or by arrowing down then pressing enter. I'm not able to like start typing the name of the playlist

 

Edit: Actually I think I got it; I checked "Alfred filters results" which seems to have fixed the not being able to type the playlist name. Not sure if that is the correct or only way to make that work.

 

Edited by Smpl
Progress Update
Link to comment
58 minutes ago, Smpl said:

Not sure if that is the correct or only way to make that work.

 

It is the correct way to make it work. The alternative would be to do the filtering yourself, which in this case is not necessary and would be slower.

Link to comment
  • 1 month later...
On 3/10/2022 at 4:30 AM, Smpl said:

 

Thanks, this works pretty well! However it only lets me select one of the 'results' from the script filter list either by pressing enter on it or the ⌘{number} keyboard shortcuts or by arrowing down then pressing enter. I'm not able to like start typing the name of the playlist

 

Edit: Actually I think I got it; I checked "Alfred filters results" which seems to have fixed the not being able to type the playlist name. Not sure if that is the correct or only way to make that work.

 

 

Have you found a way to convert the output of AppleScript to JSON format?

 

Recently I wanted to write an Alfred Workflow to activate saved layouts (which were saved in the Moom app). The Moom app now only includes limited AppleScript support, and it does not support JavaScript. 

 

I use the following commands to get a list of saved snapshots:

tell application "Moom" 
    list of snapshots 
end tell


--> {"Scapple ‧ Microsoft Edge", "Microsoft Edge ‧ Alfred Preferences"}

 

But I do not know how to convert this list into JSON FORMAT in AppleScript.

Have you found a way to convert the output of AppleScript to JSON format?

Or is there any method to deliver the list into the Script Filter in Alfred?

 

Link to comment
13 hours ago, vitor said:

 

If it supports AppleScript, it should support JXA as well.

Thank you for your reply. I just began to learn AppleScript and JavaScript on my own. I found those AppleScript in the Document of Moom app, which does not mention JavaScript. How could I convert the AppleScript to JavaScript for a specific application?

Link to comment

Luckily, I found a workflow(alfred-things workflow) that has successfully converted a list into JSON FORMAT and used it in a Script Filter just within AppleScript.

 

That's part of his code:

on add_item given title:_title, subtitle:_sub, arg:_arg, valid:_valid
	set title to "{\"title\": \"" & _title & "\", "
	set subtitle to "\"subtitle\": \"" & _sub & "\", "
	set arg to "\"arg\": \"" & _arg & "\", "
	set valid to "\"valid\": " & _valid & "}, "
	set theItem to title & subtitle & arg & valid
	set end of my _items to theItem
	return my _items
end add_item

on to_json()
	set json to "{\"items\": [" & text 1 thru -3 of (my _items as text) & "]}"
	return json
end to_json

 

I tried to adjust his code for my use, and I found it works!

 

Link to comment
9 hours ago, jierme said:

How could I convert the AppleScript to JavaScript for a specific application?

 

Apps with AppleScript support have dictionaries. You can access them via the Script Editor app (File → Open Dictionary…) or the AppleScript Dictionaries Workflow. You can change the language on the top right.


For this case, it might be something like Application("Moom").listofSnapshots().

 

2 hours ago, jierme said:

I tried to adjust his code for my use, and I found it works!

 

Glad it’s sorted!

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