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.
- Host: GitHub
- URL: https://github.com/sonsongithub/applescriptbridge
- Owner: sonsongithub
- License: mit
- Archived: true
- Created: 2024-10-05T10:05:34.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2024-11-05T12:29:00.000Z (7 months ago)
- Last Synced: 2025-02-25T05:14:50.003Z (3 months ago)
- Language: Swift
- Size: 29.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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).