Skip to content

Commit

Permalink
add a way to get sample rate via AppleScript
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentneo committed Dec 6, 2023
1 parent 991f1e0 commit e8955f9
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Quality.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
1293436B28131591002E19A8 /* CurrentUser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1293436A28131591002E19A8 /* CurrentUser.swift */; };
12AFF5C12811AD40001CC6ED /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12AFF5C02811AD40001CC6ED /* AppDelegate.swift */; };
12F1AA572868639A006C1AD8 /* DeviceMenuItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 12F1AA562868639A006C1AD8 /* DeviceMenuItem.swift */; };
BF0C90C82B20BFD6002F99C9 /* LosslessSwitcher.sdef in Resources */ = {isa = PBXBuildFile; fileRef = BF0C90C72B20BFD6002F99C9 /* LosslessSwitcher.sdef */; };
BF0C90CA2B20C163002F99C9 /* ScriptableApplicationCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF0C90C92B20C163002F99C9 /* ScriptableApplicationCommand.swift */; };
BF7E0D09296336DA009FFEEC /* AudioStreamBasicDescription+Equatable.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF7E0D08296336DA009FFEEC /* AudioStreamBasicDescription+Equatable.swift */; };
/* End PBXBuildFile section */

Expand All @@ -47,6 +49,8 @@
1293436A28131591002E19A8 /* CurrentUser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurrentUser.swift; sourceTree = "<group>"; };
12AFF5C02811AD40001CC6ED /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
12F1AA562868639A006C1AD8 /* DeviceMenuItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceMenuItem.swift; sourceTree = "<group>"; };
BF0C90C72B20BFD6002F99C9 /* LosslessSwitcher.sdef */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = LosslessSwitcher.sdef; sourceTree = "<group>"; };
BF0C90C92B20C163002F99C9 /* ScriptableApplicationCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScriptableApplicationCommand.swift; sourceTree = "<group>"; };
BF7E0D08296336DA009FFEEC /* AudioStreamBasicDescription+Equatable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AudioStreamBasicDescription+Equatable.swift"; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -110,6 +114,8 @@
127C972C281FCF000087313B /* AppVersion.swift */,
12F1AA562868639A006C1AD8 /* DeviceMenuItem.swift */,
BF7E0D08296336DA009FFEEC /* AudioStreamBasicDescription+Equatable.swift */,
BF0C90C92B20C163002F99C9 /* ScriptableApplicationCommand.swift */,
BF0C90C72B20BFD6002F99C9 /* LosslessSwitcher.sdef */,
);
path = Quality;
sourceTree = "<group>";
Expand Down Expand Up @@ -191,6 +197,7 @@
buildActionMask = 2147483647;
files = (
1272AA9F280DBB4B00FD72BA /* Preview Assets.xcassets in Resources */,
BF0C90C82B20BFD6002F99C9 /* LosslessSwitcher.sdef in Resources */,
1272AA9C280DBB4B00FD72BA /* Assets.xcassets in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -203,6 +210,7 @@
buildActionMask = 2147483647;
files = (
12AFF5C12811AD40001CC6ED /* AppDelegate.swift in Sources */,
BF0C90CA2B20C163002F99C9 /* ScriptableApplicationCommand.swift in Sources */,
1254A79C2813FB9400241107 /* Defaults.swift in Sources */,
BF7E0D09296336DA009FFEEC /* AudioStreamBasicDescription+Equatable.swift in Sources */,
1234F50E281E8F07007EC9F5 /* MediaTrack.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion Quality/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {

// https://stackoverflow.com/a/66160164
static private(set) var instance: AppDelegate! = nil
private var outputDevices: OutputDevices!
var outputDevices: OutputDevices!
private let defaults = Defaults.shared
private var mrController: MediaRemoteController!
private var devicesMenu: NSMenu!
Expand Down
4 changes: 4 additions & 0 deletions Quality/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@
<true/>
<key>NSAppleEventsUsageDescription</key>
<string>This permission is required for local file sample rate detection.</string>
<key>NSAppleScriptEnabled</key>
<true/>
<key>OSAScriptingDefinition</key>
<string>LosslessSwitcher.sdef</string>
</dict>
</plist>
10 changes: 10 additions & 0 deletions Quality/LosslessSwitcher.sdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd">
<dictionary title="LosslessSwitcher Terminology">
<suite name="LS Suite" code="sdse" description="Standard commands for this application">
<command name="get latest sample rate" code="getlrate" description="returns the last known sample rate">
<cocoa class="LosslessSwitcher.ScriptableApplicationCommand"/>
<result type="integer" description="sample rate"/>
</command>
</suite>
</dictionary>
22 changes: 22 additions & 0 deletions Quality/ScriptableApplicationCommand.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// ScriptableApplicationCommand.swift
// LosslessSwitcher
//
// Created by Vincent Neo on 6/12/23.
//

import Cocoa

class ScriptableApplicationCommand: NSScriptCommand {

override func performDefaultImplementation() -> Any? {
guard let delegate = AppDelegate.instance else {
return -1000
}
let od = delegate.outputDevices
guard let sampleRate = od?.currentSampleRate else {
return -1
}
return Int(sampleRate * 1000)
}
}

0 comments on commit e8955f9

Please sign in to comment.