def last_slide(m): applescript.run(r''' tell application "Keynote" set target to last item of (get slides of the front document) set current slide of the front document to target end tell ''')
def open_server(m): applescript.run(r''' tell application "System Events" do shell script "python -S ~/dbg/py3_dbgp -d localhost:9000 ~/taest.py" display notification "Hello world." end tell ''')
def open_dragonpad(): open_dragon_menubar() applescript.run(""" tell application "System Events" to tell process "Dragon" to tell (menu bar item 1 of menu bar 2) click menu item "Help" of menu 1 click menu item "DragonPad" of menu of menu item "Help" of menu 1 end tell """)
def _kmaestro(*_): print("Kmaestro {}".format(script_id)) applescript.run(""" tell application "Keyboard Maestro Engine" do script "{id}" end tell """.format(id=script_id))
def set_status_win(val): open_dragon_menubar() command = "Show Status Window" if val else "Hide Status Window" applescript.run(f""" tell application "System Events" to tell process "Dragon" to tell (menu bar item 1 of menu bar 2) click menu item "{command}" of menu 1 end tell """)
def website(m): w = url code = r''' tell application "Google Chrome" set t to active tab of window 1 set URL of t to "%s" end tell ''' % w applescript.run(code)
def select_menu_bar_item(m): name = str(m._words[1]) full = menu_items.get(name) if not full: return applescript.run(r""" tell application "System Events" tell (first process whose frontmost is true) click menu bar item "%s" of menu bar 1 end tell end tell """ % full)
def open_website(m): name = str(m._words[1]) w = websites.get(name) code = r''' tell application "Google Chrome" if title of active tab of window 1 is "New Tab" then set t to active tab of window 1 else set t to make new tab at end of tabs of window 1 end if set URL of t to "%s" end tell ''' % w applescript.run(code)
def update_lists(): global menu_items items = applescript.run(r""" on menubar_items() tell application "System Events" tell (first process whose frontmost is true) tell menu bar 1 set test to name of every menu bar item return test end tell end tell end tell end menubar_items set theList to menubar_items() set {text item delimiters, TID} to {",", text item delimiters} set {text item delimiters, theListAsString} to {TID, theList as text} return theListAsString """) if items is not None: items = items.split(",") else: items = [] new = {} for item in items: words = item.split(" ") for word in words: if word and not word in new: new[word] = item new[item] = item if set(new.keys()) == set(menu_items.keys()): return ctx.set_list("menu_items", new.keys()) menu_items = new
def copy_path_to_clipboard(m): x = applescript.run(""" tell application "Finder" set sel to the selection as text set the clipboard to POSIX path of sel end tell """)
def current_dir(): return applescript.run(""" tell application "iTerm" tell current session of current window variable named "session.path" end tell end tell """)
def open_dragon_pad(m): old = ctrl.mouse_pos() x = applescript.run(""" tell application "System Events" to tell process "Dragon" to tell (menu bar item 1 of menu bar 2) set AppleScript's text item delimiters to ", " position as string end tell """) x, y = map(int, x.split(", ")) ctrl.mouse(x, y) ctrl.mouse_click() ctrl.mouse(*old) applescript.run(""" tell application "System Events" to tell process "Dragon" to tell (menu bar item 1 of menu bar 2) click menu item "Help" of menu 1 click menu item "DragonPad" of menu of menu item "Help" of menu 1 end tell """)
def running_screensaver(): return ( applescript.run( """ tell application "System Events" get running of screen saver preferences end tell """ ) == "true" )
def file_manager_terminal_here(): """Opens terminal at current location""" if not is_terminal: if is_windows: actions.key("ctrl-l") actions.insert("cmd.exe") actions.key("enter") elif is_mac: from talon import applescript applescript.run(r""" tell application "Finder" set myWin to window 1 set thePath to (quoted form of POSIX path of (target of myWin as alias)) tell application "Terminal" activate tell window 1 do script "cd " & thePath end tell end tell end tell""")
def jump_slide(m): words = m.dgndictation[0]._words numbers = list(map(parse_word, words)) numbers_map = { 'zero': '0', 'one': '1', 'two': '2', 'three': '3', 'for': '4', 'four': '4', 'five': '5', 'six': '6', 'seven': '7', 'eight': '8', 'nine': '9', } numbers = [numbers_map[n] for n in numbers] number = ''.join(numbers) applescript.run(r''' tell application "Keynote" set target to item %s of (get slides of the front document) set current slide of the front document to target end tell''' % number)
def update_lists(): global menu_items items = applescript.run(update_lists_applescript) if items is not None: items = items.split(",") else: items = [] new = {} for item in items: words = item.split(" ") for word in words: if word and word not in new: new[word] = item new[item] = item if set(new.keys()) == set(menu_items.keys()): return ctx.set_list("menu_items", new.keys()) menu_items = new
def tell_alfred_flow(workflow, trigger, arg=None): argument = f' with argument "{arg}"' if arg != None else "" script = f''' tell application "Alfred 4" to run trigger "{trigger}" in workflow "{workflow}"{argument}''' # print(script) try: ret = applescript.run(script) except applescript.ApplescriptErr: print('alfred osa error:', script) raise if ret: if isinstance(ret, str) and luaDict[ret]: return luaDict[ret] print('alfred osa returned: ', ret) result = ffi.string(ret).decode('utf8') # print('osa said: ', result) return result
def tell_hammerspoon_osa(code): script = f''' tell application "Hammerspoon" return execute lua code "{code}" end tell''' # print(script) try: ret = applescript.run(script) except applescript.ApplescriptErr: print('osa error:', script) raise if ret: if isinstance(ret, str) and luaDict[ret]: return luaDict[ret] print(script) print('osa returned: ', ret) result = ffi.string(ret).decode('utf8') return result
def youtube_download(video=True): press("escape") press("cmd-l") press("cmd-c") time.sleep(0.1) url = clip.get() print(f"url: {url}") press("escape") command = f"youtube-dl " if not video: command += "--extract-audio " command += "{url}" print(f"command: {command}") return applescript.run(f""" tell application "Terminal" do script "cd {os.path.expanduser(DOWNLOAD_PATH)}; {command}; exit" end tell """)
def youtube_download(video=True): press('escape') press('cmd-l') press('cmd-c') time.sleep(0.1) url = clip.get() print(f'url: {url}') press('escape') command = f'youtube-dl ' if not video: command += '--extract-audio ' command += '{url}' print(f'command: {command}') return applescript.run(f""" tell application "Terminal" do script "cd /Users/zdwiel/Music; {command}; exit" end tell """)
def get_input_volume() -> int: return int(applescript.run("input volume of (get volume settings)"))
delay(0.1), text], "(kapeli | Cappelli)": Key("cmd-shift-space"), # Menubar: "menubar": Key("ctrl-f2"), "menubar <dgndictation> [over]": [Key("cmd+shift+/"), delay(0.1), text], "menu icons": Key("ctrl-f8"), # Bartender needed for this one "menu search": Key("ctrl-shift-f8"), # Different input volume levels "input volume high": lambda _: applescript.run("set volume input volume 90"), "input volume low": lambda _: applescript.run("set volume input volume 50"), }) ctx = Context("shortcat") ctx.keymap({ "(shortcat | short cap | shortcut) [<dgndictation>]": [ Key("cmd+shift+ctrl+f14"), text, ] }) ctx = Context("login", bundle="com.apple.loginwindow") ctx.keymap({"amigo": [keychain.find("login", "user"), Key("enter")]})
def applescript(script: str) -> str: """Run applescript""" return applescript.run(script)
def cmd(self, cmd): ret = applescript.run(f'tell application "VLC"\n{cmd}\nend tell') if ret: return ffi.string(ret).decode('utf8')
def _spotify(*_): applescript.run(f'tell application "Spotify" to {thing}')
def alfred_paste_num(num): script = f'tell application id "com.runningwithcrayons.Alfred" to run trigger "paste" in workflow "clippastes" with argument "{num}"' # print(script) ret = applescript.run(script) if ret: return ffi.string(ret).decode('utf8')
def set_input_volume(m: int): applescript.run(f"set volume input volume {m}")
def run_script(script): applescript.run(script)
def select_menu_bar_item(m): name = str(m._words[1]) full = menu_items.get(name) if not full: return applescript.run(select_menu_bar_item_applescript % full)