{"id":20,"date":"2008-03-05T11:31:16","date_gmt":"2008-03-05T11:31:16","guid":{"rendered":"http:\/\/haydnwilliams.com\/blog\/?p=33"},"modified":"2022-04-11T21:49:58","modified_gmt":"2022-04-11T20:49:58","slug":"toggle-bluetooth-with-applescript","status":"publish","type":"post","link":"https:\/\/www.haydnwilliams.com\/blog\/toggle-bluetooth-with-applescript\/","title":{"rendered":"Toggle Bluetooth with Applescript"},"content":{"rendered":"<p>I&#8217;ve just written a little Applescript to toggle the Bluetooth power on my MacBook on or off. It doesn&#8217;t take any parameters or anything, it literally just toggles it off if it&#8217;s already on, or on if it&#8217;s already off. The script looks like this:<\/p>\n<pre>#! \/usr\/bin\/osascript<\/pre>\n<pre># * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *<\/pre>\n<pre># *  Author:\tHaydn Williams<\/pre>\n<pre># *  URL:\thttp:\/\/www.haydnwilliams.com<\/pre>\n<pre># *  Date:\tMarch 2008<\/pre>\n<pre># *  Script:\ttoggleBluetooth.scpt<\/pre>\n<pre># *  Purpose:\tToggle bluetooth status<\/pre>\n<pre># *  Useage:\ttoggleBluetooth.scpt<\/pre>\n<pre>#  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *<\/pre>\n<pre>tell application \"System Preferences\"<\/pre>\n<pre>\tactivate<\/pre>\n<pre>\tset current pane to pane \"com.apple.preferences.Bluetooth\"<\/pre>\n<pre>\ttry<\/pre>\n<pre>\t\ttell application \"System Events\"<\/pre>\n<pre>\t\t\ttell process \"System Preferences\"<\/pre>\n<pre>\t\t\t\tclick checkbox \"On\" of window \"Bluetooth\"<\/pre>\n<pre>\t\t\tend tell<\/pre>\n<pre>\t\tend tell<\/pre>\n<pre>\ton error<\/pre>\n<pre>\t\tdisplay dialog \"Failed to toggle bluetooth.\"<\/pre>\n<pre>\tend try<\/pre>\n<pre><span class=\"Apple-tab-span\" style=\"white-space: pre\">\t<\/span>tell application \"System Preferences\" to close the first window<\/pre>\n<pre>end tell<\/pre>\n<p><br class=\"webkit-block-placeholder\" \/>You can download the compiled script from\u00a0<a href=\"http:\/\/haydnwilliams.com\/blog\/wp-content\/uploads\/2008\/03\/togglebluetooth.scpt\" title=\"Applescript to toggle Bluetooth power\">here<\/a>. Please note that you&#8217;ll need to &#8216;<span class=\"Apple-style-span\" style=\"font-family: 'Courier New'; font-size: 11px; line-height: 13px; white-space: pre\">Enable GUI Scripting<\/span>&#8216; in the AppleScript Utility (you can find it in the AppleScript folder of Applications). To run the script, just double-click on it. Please note that this will probably only work on English language systems, but should be easy enough to alter.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve just written a little Applescript to toggle the Bluetooth&#46;&#46;&#46;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[18,37,185,186,343],"class_list":["post-20","post","type-post","status-publish","format-standard","hentry","category-it","tag-applescript","tag-bluetooth","tag-mac","tag-macbook","tag-toggle"],"_links":{"self":[{"href":"https:\/\/www.haydnwilliams.com\/blog\/wp-json\/wp\/v2\/posts\/20","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.haydnwilliams.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.haydnwilliams.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.haydnwilliams.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.haydnwilliams.com\/blog\/wp-json\/wp\/v2\/comments?post=20"}],"version-history":[{"count":0,"href":"https:\/\/www.haydnwilliams.com\/blog\/wp-json\/wp\/v2\/posts\/20\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.haydnwilliams.com\/blog\/wp-json\/wp\/v2\/media?parent=20"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.haydnwilliams.com\/blog\/wp-json\/wp\/v2\/categories?post=20"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.haydnwilliams.com\/blog\/wp-json\/wp\/v2\/tags?post=20"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}