Jump to content

osascript runs in terminal but nothing happens when triggered by Alfred


Recommended Posts

Posted
I wrote this osascript to open "Preview" and select File->New From Clipboard.

I tested it in terminal and it successfully does so.

But when I trigger it with Alfred, nothing happens. Alfred debugger shows 

[05:56:25.914] Logging Started...
[05:56:29.144] Save Clipboard to File[Keyword] Processing complete
[05:56:29.147] Save Clipboard to File[Keyword] Passing output '' to Run Script

I'm not sure how to get osascript logs to also print in Alfred debugger. My custom logs don't seem to be in the debugger at all.

Here is my script

on run argv
  log "running workflow"
  set appToCall to ""

  if count of argv = 1 then
    set appToCall to item 1 of argv
  end if 

  if appToCall = "" then
    set appToCall to "Preview"
  end if

  set retryCount to 0
  set maxRetries to 5

  repeat until application appToCall is frontmost or retryCount >= maxRetries
      delay 0.5
      set retryCount to retryCount + 1
      tell application appToCall to activate
  end repeat

  tell application "System Events"
    log "sending to app"
    tell process appToCall
      if appToCall = "Preview" then
        log "app is Preview"
        click menu item "New from Clipboard" of menu "File" of menu bar 1
      end if
    end tell
  end tell
end run

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