Jump to content

Script Filter using an App (Numbers|Safari) to build the list requires focus on that App before it is invoked to work correctly


Recommended Posts

I am trying to debug (my first) script filter. I am using the option where Alfred filters the results (screenshot attached). If I understand correctly that means that my script runs only once.

 

On my Mac I already have Numbers running with a number of sheets open that are being shown with those windows merged in a tab view before I invoke the workflow.

 

My script filter uses AppleScript (getSheetList.scpt) to query Numbers for the names of those sheets.

 

Scenarios:

  1. If I have focus on Numbers when I launch my workflow, the list is populated with the sheet names, as expected, and there is no issue.
     
  2. However, if the focus is elsewhere, the first time that I run the workflow the only response is to shift focus to Numbers - no list is generated - and the workflow ends. 
  • The second time I run the workflow all runs as expected, with a list being returned, since the first invocation set up the focus.

 

I thought that I just had a focus issue so I investigated that further ...

 

Attempts to force focus:

  1. If I manually force focus from Script Editor by running

    tell application "Numbers" to activate

    the workflow produces a list and there is no issue
     

  2. If I try to force focus by adding
    open -a Numbers

    or 

    osascript -e 'activate application “Numbers”'

    to my filter script then Numbers gets focus, but no list is generated.

 

I can't see what to do at this point, so here is the debug information that hopefully will help someone steer me in the right direction.

 

Alfred debug window:

 

  1. for a "failed" run
    [15:28:57.162] Numbers Sheets - pmf[Script Filter] Queuing argument '(null)'

     

  2. for a "successful" run
    [15:30:36.081] Numbers Sheets - pmf[Script Filter] Queuing argument '(null)'
    [15:30:36.389] Numbers Sheets - pmf[Script Filter] Script with argv '(null)' finished
    [15:30:36.392] Numbers Sheets - pmf[Script Filter] {"items": [{"uid":"nBaseline Running Costs","title":"re_20 ...

 

 

Any help would be much appreciated.

screenshot.png

Link to comment
5 hours ago, PaulF said:

(screenshot attached)


(…)

 

My script filter uses AppleScript (getSheetList.scpt)


Please post your Workflow somewhere when asking for help. It’s not possible to debug your issue from a mostly empty screenshot and the name of a script. We have zero idea of what your code is doing.

Link to comment

Thanks @vitor for responding. I have just simplified my AppleScript code to remove some extraneous code that was very specific to my process.

 

This workflow fails in the same way as before. The keyword for this public version of the workflow is 'nsp'.

 

This is my first time sharing a workflow so I hope this dropbox link works for you.

 

https://www.dropbox.com/s/9vzrll6pfjx8t81/Numbers Sheets - public.alfredworkflow?dl=0

 

Thanks again for offering to take a look.

Link to comment
54 minutes ago, PaulF said:

Thanks again for offering to take a look.

 

Normally that’s what I’d be doing, but I don’t use Numbers and this seems specific to it. My advice was so the next person—who will be able to help—has the necessary details.

Link to comment

I have a very similar workflow with the same structure that works with Safari instead of Numbers.

 

This workflow shows the same behavior, requiring Safari to be in focus when the workflow is invoked in order for it to present the list of open tabs.

 

Otherwise it just shifts the focus to Safari. Running a second time then works as expected because Safari now has focus.

 

https://www.dropbox.com/s/e9g6oxom4igvln9/Safari Tabs - pmf.alfredworkflow?dl=0

 

Thanks for your help.

Link to comment
  • PaulF changed the title to Script Filter using an App (Numbers|Safari) to build the list requires focus on that App before it is invoked to work correctly
1 hour ago, PaulF said:

This workflow shows the same behavior, requiring Safari to be in focus when the workflow is invoked in order for it to present the list of open tabs.

 

Then there's something wrong with your machine. The app needs to be running to talk to it via AppleScript, but it shouldn't need to be active.

 

Here's a version of your workflow that works for me (at least as far as I understood your description).

 

I’ve rewritten it in JXA/JavaScript because you cannot generate valid JSON in AppleScript (unless you import and use Objective-C libraries).

 

Here's the code to show a list of Numbers sheets in Alfred:

 

#!/usr/bin/osascript -l JavaScript

function getSheets() {
  const numbers = Application('Numbers')
  let sheets = []

  ObjC.unwrap(numbers.documents()).forEach(doc => {
    let fullName  = doc.name(),
        docPath   = doc.file()
        // remove file extension
        shortName = fullName.replace(/\.numbers$/, '')

    // convert Path objects to normal string POSIX paths
    docPath = docPath ? docPath.toString() : null

    ObjC.unwrap(doc.sheets()).forEach(sheet => {
      sheets.push({docName:      fullName,
                   docShortName: shortName,
                   docPath:      docPath,
                   sheetName:    sheet.name()})
    })
  });
  return sheets
}

// script entry point
function run() {
  let items = []
  // get list of sheets and convert them to Alfred's feedback format
  getSheets().forEach(sheet => {
    items.push({
      title:    `${sheet.docShortName}  ${sheet.sheetName}`,
      subtitle:  sheet.docPath || '<unsaved>',
      // set a UID based on filepath (if set) so Alfred can learn based on
      // your selections
      uid:       sheet.docPath ? sheet.docPath + '.' + sheet.sheetName : null,
      // if arg is a valid filepath, and type is set to "file", you can
      // perform File Actions on the result
      arg:       sheet.docPath,
      type:      sheet.docPath ? 'file' : null,
      valid:     true,
      // stick all the sheet data in workflow variables
      variables: sheet
    })
  })
  return JSON.stringify({items: items}, null, 2)
}

 

Link to comment

Thanks for the quick response and your clean JXA code.

 

I have tried your "really fixed" workflow but unfortunately the behavior is the same for me as my original workflow.

 

Does my workflow show the symptoms that I describe - fails, but only when Numbers is not in focus?

 

If so, then maybe my machine is broken but I have no idea in what way and how I might proceed to "fix" it 😞

 

 

Link to comment
14 minutes ago, PaulF said:

Does my workflow show the symptoms that I describe - fails, but only when Numbers is not in focus?

 

No. Like I said, if that's happening, there's something up with your OS. That is not normal. Have you tried rebooting?

Link to comment

I did just reboot to be sure, but the behavior is the same.

 

I have been running this workflow for some time with the initial glitch and only yesterday spent the time to characterize the problem - i.e. fails first time unless Numbers is in focus.

 

I see no difference in behavior between my AppleScript based WF and your JXA version. They both produce the same results when they succeed. But they both fail (for me) if I have the Alfred Preferences Window, or any other window other than Numbers, in focus when I run either workflow. As I have said the second time is then successful in both cases so long as I keep Numbers in focus.

 

I get the same behavior with my Safari Tabs workflow as well so it doesn't seem to be app-specific. 🤔

 

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