Get a menu list loop without interruptions


use AppleScript version "2.4"
use scripting additions

property listOptions : {"Say Hello", "Say Good morning", "Goodbye"}

repeat -- Infinite loop
	set option to ""
	
	repeat while option is ""
		set option to choose from list listOptions with title "General menu…" with prompt "Choose an option…"
		if option is false then return
		set option to item 1 of option
		
		if option is "Goodbye" then
			say "Goodbye"
			return
		end if
	end repeat
	
	if option is "Say Hello" then
		say "Hello"
	else if option is "Say Good morning" then
		say "Good morning"
	end if
	
end repeat

The result is as expected: a loop without interruptions until the output option is chosen.

However, in this other situation involving “System Events” and “Finder”, when a loop ends, the menu list disappears and the Script Editor icon makes small jumps in the Dock, When clicking on it, the menu list reappears allowing to continue a new loop until the termination condition is met (the “Exit” option).

use AppleScript version "2.4"
use scripting additions

property listOptions : {"Open Applications Folder in Tab", "Open Downloads Folder in Tab", "Exit"}
property pathHFS : missing value
property option : missing value

repeat -- infinite loop
	set option to ""
	repeat while option is ""
		
		set option to choose from list listOptions with title "General menu…" with prompt "Choose an option…"
		if option is false then return
		set option to item 1 of option
		if option is "Exit" then return
	end repeat
	
	if option is "Open Applications Folder in Tab" then
		set pathHFS to (path to applications folder)
		my folderInTab(pathHFS)
		
	else if option is "Open Downloads Folder in Tab" then
		set pathHFS to (path to downloads folder)
		my folderInTab(pathHFS)
		
	else if option is "Exit" then
		return
	end if
	
end repeat


on folderInTab(_pathHFS)
	tell application "System Events"
		set frontmost of process "Finder" to true
		keystroke "t" using command down
		delay 0.1
		tell application "Finder" to set target of front window to _pathHFS
	end tell
end folderInTab

The objective is to achieve that, after having finished the execution of an option, the menu list reappears but without the interruptions that occur after the end of each cycle of the loop.

Any suggestions?

I appreciate any help.

Try this…

use AppleScript version "2.4"
use scripting additions

property listOptions : {"Open Applications Folder in Tab", "Open Downloads Folder in Tab", "Exit"}

local option, pathHFS
repeat
	activate -- brings app that is currently running this script to front
	set option to choose from list listOptions with title "General menu…" with prompt "Choose an option…"
	if option is false then exit repeat
	set option to item 1 of option
	if option is "Exit" then
		exit repeat
	else if option is "Open Applications Folder in Tab" then
		set pathHFS to (path to applications folder)
	else if option is "Open Downloads Folder in Tab" then
		set pathHFS to (path to downloads folder)
	end if
	folderInTab(pathHFS)
end repeat

on folderInTab(_pathHFS)
	tell application "System Events"
		set frontmost of process "Finder" to true
		keystroke "t" using command down
	end tell
	delay 0.1
	tell application "Finder" to set target of front window to _pathHFS
end folderInTab

I cleaned up the subroutine ‘folderInTab’ by moving things that don’t need to be in the tell block to outside the tell block

BTW, since “Exit” does the same as clicking “Cancel”, why have it at all? It’s repetitive.

Like so…

use AppleScript version "2.4"
use scripting additions

property listOptions : {"Applications", "Downloads", "Documents"}

local option, pathHFS
repeat
	activate -- brings app that is currently running this script to front
	set option to choose from list listOptions with title "Choose a folder…" with prompt "Choose an folder to open in new Finder tab…"
	if option is false then exit repeat
	set option to item 1 of option
	if option is "Documents" then
		set pathHFS to (path to documents folder)
	else if option is "Applications" then
		set pathHFS to (path to applications folder)
	else if option is "Downloads" then
		set pathHFS to (path to downloads folder)
	end if
	folderInTab(pathHFS)
end repeat

on folderInTab(_pathHFS)
	tell application "System Events"
		set frontmost of process "Finder" to true
		keystroke "t" using command down
	end tell
	delay 0.1
	tell application "Finder" to set target of front window to _pathHFS
end folderInTab
1 Like

Thank you very much, robertfern, for your quick and efficient help.
It not only solved the problem, but also helped me to point out my mistakes and learn how to correct them.

My regards and gratitude for your time and knowledge.

I also have a version that you can choose more than 1 folder at a time.

Like so…

use AppleScript version "2.4"
use scripting additions

property listOptions : {"Applications", "Downloads", "Documents"}

local option, pathHFS, listString, ans, tid
set {tid, text item delimiters} to {text item delimiters, return}
set listString to listOptions as text
set text item delimiters to tid
activate -- brings app that is currently running this script to front
set ans to choose from list listOptions with title "Choose a folder…" with prompt "Choose an folder to open in new Finder tab…" with multiple selections allowed
if ans is false then return
repeat with option in ans
	set pathHFS to path to item (count paragraphs of (text 1 thru (offset of option in listString) of listString)) of {applications folder, downloads folder, documents folder}
	folderInTab(pathHFS)
end repeat

on folderInTab(_pathHFS)
	tell application "System Events"
		set frontmost of process "Finder" to true
		keystroke "t" using command down
	end tell
	delay 0.1
	tell application "Finder" to set target of front window to _pathHFS
	delay 0.3
end folderInTab

EDIT - I changed an ‘exit repeat’ to ‘return’ because it was no longer in a repeat loop

1 Like

Thank you very much for your suggestion. It is very interesting and enriching. I will incorporate it into the project.

I would like to take this opportunity to ask if there is an alternative code to the one I am using to refer to the “computer container” folder.
The encoding used for the “computer container” folder could not benefit from the folderInTab(_pathHFS) handler and should be treated specifically for it.
That is the reason to get an encoding for the “computer container” folder compatible with folderInTab(_pathHFS).

(Sorry I don’t know the English menus so I can avoid the corresponding code for the Spanish Finder menus).

on computerContainerTab()
	tell application "Finder" to activate
	tell application "System Events" to tell process "Finder"
		keystroke "t" using command down
		delay 0.1
		click menu item "Ordenador" of menu "Ir" of menu bar 1  -- ###
	end tell
end computerContainerTab

The English version is this…

on computerContainerTab()
	tell application "Finder" to activate
	tell application "System Events" to tell process "Finder"
		keystroke "t" using command down
		delay 0.1
		click menu item "Computer" of menu "Go" of menu bar 1
	end tell
end computerContainerTab

:grinning: I have not explained myself well or you have not understood me.

I wanted to ask for an alternative code to this one that would be compatible with the folderInTab(_pathHFS) handler; put another way, my question is how to express the pathHFS path of the “computer container” folder and avoid using the GUI.

On the other hand, it was a great effort for me to get to understand the following part of the code

set pathHFS to path to item (count paragraphs of (text 1 thru (offset of option in listString) of listString)) of {applications folder, downloads folder, documents folder})

(the path will be the item in the list {applications folder, downloads folder, documents folder} indicated by the paragraph number in which the first character of the “option” variable being parsed from the “ans” list is located).

I think that the approach is only valid for those special system folders (desktop, pictures, documents, downloads, library, etc.) but not for the paths of the user’s personal folders and I can’t find an alternative for a code that allows a multiple choice of user’s folders.

The only thing that seems to work is this…

Tell application “Finder” to open computer container

This will open the computer in a new window.
You can’t set an existing window or tab to the computer.
Or it’s target

Thank you, robertfern, for responding.

I wrote your suggestion in the Script Editor and it did a rewrite of that code, turning it into…

tell application "Finder" to open (system attribute object)

which, in turn, gave a compiler error →

“The variable object is not defined”.

Hi robertfern.
I made some adjustments to your fixes so that also the user folders work in the multiple selection of items in the list.
However, I can’t figure out how to integrate the implementation of the options for the “Computer” and “Recent” folders, which need to be treated with a different handler.

use AppleScript version "2.4"
use scripting additions

property listOptions : {"Applications", "Kingston", "Documents"}
--property listOptions : {"Applications", "Kingston", "Computer", "Recent"}

local option, pathHFS, listString, ans, tid, listaGeneralPaths

set nUser to short user name of (system info)

set path1 to path to applications folder
set path2 to POSIX file ("/Users/" & nUser & "/Kingston") as alias
set path3 to path to documents folder
set listGeneralPaths to {path1, path2, path3}

repeat
	set {tid, text item delimiters} to {text item delimiters, return}
	set listString to listOptions as text
	set text item delimiters to tid
	
	activate -- brings app that is currently running this script to front
	set ans to choose from list listOptions with title "Choose a folder…" with prompt "Choose an folder to open in new Finder tab…" with multiple selections allowed
	if ans is false then return
	
	repeat with option in ans
		(*
		if option is "Computer" then
			computerTab()
		else if option is "Recent" then
			recentTab()
		else
			*)
		
		set pathHFS to item (count paragraphs of (text 1 thru (offset of option in listString) of listString)) of listGeneralPaths
		folderInTab(pathHFS)
		
		--end if
	end repeat
end repeat


on folderInTab(_pathHFS)
	tell application "System Events"
		set frontmost of process "Finder" to true
		keystroke "t" using command down
	end tell
	delay 0.3 --  ++
	tell application "Finder" to set target of front window to _pathHFS
	delay 0.5 --  ++
end folderInTab


on computerTab()
	tell application "Finder" to activate
	tell application "System Events" to tell process "Finder"
		keystroke "t" using command down
		delay 0.2
		click menu item "Computer" of menu "Go" of menu bar 1
	end tell
end computerTab


on recentTab()
	tell application "Finder" to activate
	tell application "System Events" to tell process "Finder"
		keystroke "t" using command down
		delay 0.2
		keystroke "f" using {command down, shift down}
	end tell
end recentTab

I’m sorry for the compile error. But if you’d looked, you’d have seen that I edited my post to fix that.

:grinning: You are right. :+1: