An open API service indexing awesome lists of open source software.

https://github.com/sonsongithub/applescriptbridge

Swift code to manage "Music.app" using AppleScript.
https://github.com/sonsongithub/applescriptbridge

Last synced: 29 days ago
JSON representation

Swift code to manage "Music.app" using AppleScript.

Awesome Lists containing this project

README

        

**This project is archived. Please refer to [the new project](https://github.com/sonsongithub/MusicConMenu).**

# AppleScriptBridge
Swift code to manage "Music.app" using AppleScript.

# Capability and privacy setting

At first, you have to add the following privacy authentication phrase to the `Info.plist`.

## Permit the application to use AppleEvent.

## Permit to communicate with Music.app.

# ScriptBridge

## Create Music.h using `sdef`

```
sdef /System/Applications/Music.app | sdp -fh --basename Music
```

## Import header file

Added Music.h to the Xcode project.

## Create Bridging Header

At first, create a dummy objective-c sources and then Xcode will ask you to create a bridging header.

You can add the following line to the bridging header.

```objc
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//

#import "Music.h"
```

## Prepare interface to write Swift code

You have to prepare the interface to write Swift code.
But, I've already implemented [the python script](https://github.com/sonsongithub/AppleScriptBridge/blob/main/converter.py) to extract the Swift interface from the header file.

```bash
python converter.py ./AppleScriptBridge/Music.h ./AppleScriptBridge/Music.swift
```

## Write Swift code

```swift
if let musicApp: MusicApplication = SBApplication(bundleIdentifier: "com.apple.Music") {
if let song = musicApp.currentTrack {
print(song.name)
print(song.artist)
}
}
```

# NSAppleScript

We can also use `NSAppleScript` to execute AppleScript.
This way requres us to write AppleScript codes and implement some classes or structs to handle the result. Refer to [the example](https://github.com/sonsongithub/AppleScriptBridge/blob/main/AppleScriptBridge/AppleScriptManager.swift).