exposition Posted June 15, 2023 Share Posted June 15, 2023 (edited) Hi, I found a simple script online to toggle my Mac's connection to my AirPods on and off. When I run the script in Script Editor, it runs as expected (i.e., the connection toggles on and off). But when I've tried to add the same script to Alfred, it works only after my having first run the script from within Script Editor to toggle the connection off. I don't know much about scripting, so I am at a loss wrt troubleshooting. Here is the code: Quote use framework "IOBluetooth" use scripting additions set AirPodsName to "My AirPods Pro" on getFirstMatchingDevice(deviceName) repeat with device in (current application's IOBluetoothDevice's pairedDevices() as list) if (device's nameOrAddress as string) contains deviceName then return device end repeat end getFirstMatchingDevice on toggleDevice(device) if not (device's isConnected as boolean) then device's openConnection() return "Connecting " & (device's nameOrAddress as string) else device's closeConnection() return "Disconnecting " & (device's nameOrAddress as string) end if end toggleDevice return toggleDevice(getFirstMatchingDevice(AirPodsName)) The code as it looks when pasted into Alfred is attached below. I'm calling the script with a keyword and no argument. And this is the output of the Alfred Workflow debugging console: Quote [19:56:25.112] Logging Started... [19:56:27.497] toggle airpods connection[Keyword] Processing complete [19:56:27.498] toggle airpods connection[Keyword] Passing output '' to Run Script Any help would be much appreciated. Thanks! Edited June 15, 2023 by exposition Link to comment
vitor Posted June 15, 2023 Share Posted June 15, 2023 Welcome @exposition, We can look further at the code (or, more specifically, the full workflow) to see where the problem may lie. But are you trying to get that code to work or do are you looking for any solution to toggle AirPods? If the former, connect something else to your workflow (say, a Debugger Utility) to see what’s coming out of that script, since it appears to output a message. If the latter, there’s a workflow in the Alfred Gallery which will do that for you. Be sure to run the dependencies manager once that’s installed. Link to comment
exposition Posted June 15, 2023 Author Share Posted June 15, 2023 @vitor: Thanks for your reply! If possible, I'd like to use this code. It's simple and doesn't rely on external dependancies (which may break in future Mac OS updates). If, however, getting it to work proves very difficult, I am okay to use the workflow you provided. I will connect the Debugger Utility to the workflow today and see what it has to say. Thanks again. Link to comment
vitor Posted June 15, 2023 Share Posted June 15, 2023 2 minutes ago, exposition said: doesn't rely on external dependancies (which may break in future Mac OS updates). Possible but unlikely in this case. blueutil (the dependency) uses the same framework as the code you posted. Link to comment
exposition Posted June 15, 2023 Author Share Posted June 15, 2023 Ah, that's reassuring! Link to comment
exposition Posted June 15, 2023 Author Share Posted June 15, 2023 @vitor Looking at the Debugger Utility's output, it appears that the script, rather than toggling the connection between connected and disconnected, sends 'Disconnecting My AirPods Pro' each and every time. Unless this is a very easy fix, to avoid taking up too much of your time, I'm inclined to try Acidham's AirPods Connector workflow. Thanks! Quote [07:23:12.744] toggle airpods connection[Run Script] Processing complete [07:23:12.757] toggle airpods connection[Run Script] Passing output 'Disconnecting My AirPods Pro ' to Debug [07:23:12.760] toggle airpods connection[Debug] 'Disconnecting My AirPods Pro ', {} [07:23:19.681] toggle airpods connection[Keyword] Processing complete [07:23:19.684] toggle airpods connection[Keyword] Passing output '' to Run Script [07:23:19.922] toggle airpods connection[Run Script] Processing complete [07:23:19.934] toggle airpods connection[Run Script] Passing output 'Disconnecting My AirPods Pro ' to Debug [07:23:19.936] toggle airpods connection[Debug] 'Disconnecting My AirPods Pro ', {} [07:23:26.895] toggle airpods connection[Keyword] Processing complete [07:23:26.898] toggle airpods connection[Keyword] Passing output '' to Run Script [07:23:27.138] toggle airpods connection[Run Script] Processing complete [07:23:27.148] toggle airpods connection[Run Script] Passing output 'Disconnecting My AirPods Pro ' to Debug [07:23:27.151] toggle airpods connection[Debug] 'Disconnecting My AirPods Pro ', {} Link to comment
vitor Posted June 18, 2023 Share Posted June 18, 2023 I suspect this will be happening due to a difference I’ve encountered before where executing framework code via Script Editor or osascript behaves differently (though in theory it should be the same). Save the code to a file and run it with osascript /PATH/TO/FILE. You’ll probably get the same (non-working) result. This is not something Alfred has control over, since it’s related to Apple’s side of the equation. You can try on a Run NSAppleScript Action (generally best avoided in favour of the method you’re using now) to see if that works, but I’d recommend @Acidham’s workflow, who is a regular on the forum and is likely to handle an eventual breakage (which won’t happen with code copied from a page). Link to comment
exposition Posted June 18, 2023 Author Share Posted June 18, 2023 Thanks. Yes, it sounds like @Acidham's solution is better. I see that I require Python3, which I do not have installed on my machine; is there a preferred way to get it? It seems from searching Google that pyenv is recommended? https://github.com/pyenv/pyenv Link to comment
Stephen_C Posted June 18, 2023 Share Posted June 18, 2023 Just now, exposition said: is there a preferred way to get it? Download the workflow from the gallery and you will automatically be guided for the necessary supplemental installation. Stephen Link to comment
vitor Posted June 18, 2023 Share Posted June 18, 2023 @exposition You only need to start using the workflow. It uses the system’s Python 3 so if you don’t have it you’ll get a macOS GUI prompt to install the Developer Tools. It takes two clicks and you’re done. Link to comment
exposition Posted June 20, 2023 Author Share Posted June 20, 2023 (edited) Thank you both. I installed the workflow, and it seems so far to be working. It didn't prompt me to install the Developer Tools, so maybe I already had them? Thanks again! Edited June 20, 2023 by exposition Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now