Question

Outline:

  1. I created a list of all running apps (activeProcesses)
  2. I want it to match against a predefined list of text editors (appList)
  3. I in activeProcesses are apps from appList create a new list (activeEditorList) => 1st Problem
  4. I want to match how many text editors from activeEditorList are running in activeProcesses
    • matching the lists is my 2nd problem
  5. Output or Cancel
    • If only on text editor is running from appList I want to return it's name
    • If more than one text editor is running, prompt me to select one from activeEditorList
    • If none is running Cancel

Here's the first part of my script: create a new list out of all running apps. The final piece of the puzzle is missing. My ouput reads:

{item 1 of {"nvALT", "FoldingText", "Byword"}, item 2 of {"nvALT", "FoldingText", "Byword"}}`

instead of {"nvALT", "iTerm"}

Here's my script:

set activeEditorsList to {}
set appList to {"nvALT", "FoldingText", "Byword"}

--    Generate variable with running apps
tell application "System Events"
    set activeProcesses to (name of every process)
end tell

--    Generate list of running text editors    
repeat with appName in appList
    if (activeProcesses contains appName) is true then
        set activeEditor to appName
        copy appName to the end of activeEditorsList
    end if
end repeat
return activeEditorsList

Promblem 1: Line 12 set activeEditor to appName doesn't work and my fruitless attempts of correcting don't work either.


When the activeEditorsList variable is ready I want to use it again to find out if only one text editor is running (=> return the name) or more than one is running (=> prompt to choose one from the list and return its name).

This is how I imagined the matching to look like:

set appList to {"nvALT", "FoldingText", "Byword"}
set activeEditorsList to {"nvALT", "Byword"}

on count_matches(this_list, this_item)
    set the match_counter to 0
    repeat with i from 1 to the count of this_list
        if item i of this_list is this_item then ¬
            set the match_counter to the match_counter + 1
    end repeat
    return the match_counter
end count_matches

count_matches(appList, (items of activeEditorsList))

Promblem 2: To problem with the code above is that in the last line (items of activeEditorsList) doesn't work as I expected it to work – although putting "nvALT" in there works.


Then I need to write something along the lines… if count_matches 0 then cancel, elif 1 return name, else prompt to choose from list.

I have yet no idea how to match the count, but I think I can figure it out (with time). The prompt on the other hand is a small thing I've already prepared:

set activeEditorsList to {"nvALT", "Byword"}

tell me to set selectedEditor to choose from list activeEditorsList
return selectedEditor as text

I'd be thankful for a bit of guidance.

Was it helpful?

Solution

Adding a second answer to address the script as a whole instead of just the single question.

For the first part of the script, you just need to get a list of pre-defined applications that are currently running. You already have that part working right:

set activeEditorsList to {}
set appList to {"nvALT", "FoldingText", "Byword"}

--    Generate variable with running apps
tell application "System Events"
    set activeProcesses to (name of every process)
end tell

--    Generate list of running text editors    
repeat with appName in appList
    if appName is in activeProcesses then
        set end of activeEditorsList to appName
    end if
end repeat

For the second part, you don't need to match anything. You've already done that with activeEditorsList. You just need to find out how many you've matched and work from there, using Applescript's count command

editorCount = (count activeEditorsList)
if editorCount = 1 then
    return item 1 of activeEditorsList
else if editorCount > 1 then
    return choose from list activeEditorsList
else
    -- Handle 0 items
end if

As a side note, for the case of 0 editors, it sounds like this code is in a sub-routine. You should probably either throw an error, or return a null-type value (null, missing value, ""), and cancel the script where that routine is called.

OTHER TIPS

(per my comment, I will only be addressing your first problem)

I'm not sure what doesn't work about set activeEditor to appName. You're setting the value of the variable activeEditor to the name of an app. This is working for me. (I've also made some superficial edits to the code.)

set activeEditorsList to {}
set appList to {"Sublime Text 2"}

tell application "System Events"
    set activeProcesses to (name of every process)
end tell

repeat with appName in appList
    if appName is in activeProcesses then
        set activeEditor to appName
        set end of activeEditorsList to appName
    end if
end repeat
activeEditor
--> {item 1 of {"Sublime Text 2"}}

If you're trying to actually activate that editor, that is not the way to do it. Again, you are just assigning to a variable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top