1

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.

2
  • matching the lists is my 2nd problem. You should split the second problem into a second question. Apr 1, 2014 at 15:18
  • @darrick-herwehe I tought about that, too, but then again there might be someone smarter out there who suggest approaching my main goal completely different. Then I'd miss some smart details and gaining more insight into the world of AppleScript.
    – patrick
    Apr 1, 2014 at 15:24

2 Answers 2

0

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.

2
  • Thanks Darrik, this indeed renders matching useless and looks much simpler than what I had in mind. I had to define editor Count and replaced it with set editorCount to count activeEditorsList to work. Still… what I had in mind was passing the result to another app. Therefore my output/result should be the name of item x, c.f. "nvALT" not item 1 of {"nvALT", "FoldingText", "Byword", "Mou"}… which brings us back to problem 1. Almost there, do know a way to get just the name not the item reference?
    – patrick
    Apr 1, 2014 at 16:29
  • Okay, got it: return item 1 of activeEditorsList as text
    – patrick
    Apr 1, 2014 at 16:36
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.