Jump to content

AppleScript running successfully in Script Editor but not working as expected in Alfred


Recommended Posts

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. alfredscreenshot.png.73d43ccc8f71ad1208b6d7adc3f90533.png

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 by exposition
Link to comment

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

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

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

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