Putting a list generated by the Finder into a popup?

Hello,

I am trying to get all mounted volumes and place them in a popup. You can see my attempt below does not work. I have 2 questions.

  1. How do I populate my popup with the list that is created?
  2. How do I know what item in the popup the user selected and put it into a shell script?

Thank you,

Damon

on should close theObject
	quit application "MiG"
end should close

--defines buttons task
on clicked theObject
	
	if name of theObject is "partition_button" then
		tell application "Disk Utility" to activate
		
	else if name of theObject is "image_button" then
		-- do shell script ASR ...
	end if
	
end clicked




--sets target_popup
set excludeDisks to {"BootCD", "Network"}

tell application "Finder" to set dlist to (name of every disk whose name is not in excludeDisks) as list


set contents of "target_popup" to "dlist"

--Popups

on action theObject
	if the name of theObject is "target_popup" then
		do something
	end if
end action

Sorry I did not do a search befor posting.

Can someone explain how the code jobu posted works.

on choose menu item theObject --> Attached to the MENU, not it's items 
   set theMenuItemTitle to title of current menu item of theObject 
   set theMenuItemName to name of current menu item of theObject 
    
   if theMenuItemName is not "Default" then 
      display dialog (theMenuItemTitle as string) 
   end if 
end choose menu item 

on addPopupItem(theItem, theName, thePopup) 
  make new menu item at the end of menu items of menu of thePopup with properties {title:theItem,name:theName, enabled:true} 
end addPopupItem

The example code you have was pretty specialized for one application. This code is much simpler and will take every item in a list and place it into a popup menu…

set myList to {"Item 1", "Item 2", "Item 3"}

tell menu of popup button "theButton" of window "theWindow"
	try --> Try to delete any existing menu items, if desired
		delete every menu item
	end try
			
	repeat with tempItem in myList
		make new menu item at end of menu items with properties {name:tempItem, title:tempItem, enabled:true}
	end repeat
end tell

The ‘choose menu item’ handler is attached to either a menu or a menu item. If you attach it to a menu, it calls the handler every time a menu item is selected. Note that in this case “theObject” is the menu itself, so you must use code similar to that which you posted to evaluate which menu item was selected. If you attach it to an individual menu item, it is called only when that item is selected. In doing this ‘theObject’ is the menu item, so you use something like “set theObjectName to name of theObject”

The “addPopupItem()” subroutine was part of the original posters code, and is not necessarily the only/best way. It depends on your application. The code I posted above does the same thing, only without using a subroutine.

Hope that helps…
j

Thank you for the reply Jobu.
I am still having issues making this work. Please bear with me as I am starting again after a long break from scripting and this is my first attemt at AS studio.

A few questions? Is the list below true
myList= is generatd by me I can change “Item 1” to “My Item 1” and it will display My item 1in the popup

theButton=is the name I give the popup list in Get Info-> AppleScript ->Script Should it be called button or thebutton

theWindow= my window, I only have 1that is named “Window” in Get Info-> AppleScript ->Script Do I have to check any actions to take place in this area

tempItem= How and why is this used

Sorry for so many questions. I am just trying to learn.

Thank you,

Damon

set myList to {"Item 1", "Item 2", "Item 3"}

tell menu of popup button "theButton" of window "theWindow"
	try --> Try to delete any existing menu items, if desired
		delete every menu item
	end try
			
	repeat with tempItem in myList
		make new menu item at end of menu items with properties {name:tempItem, title:tempItem, enabled:true}
	end repeat
end tell

Yes, the “myList” variable is a generic list that will be read and each item inserted into the popup menu. If using the syntax from your first post, this would actually be changed to “dlist” I believe.

These are just names I gave the objects in the sample code to illustrate the references I was making. You can change them to whatever you want in interface builder and then make sure to reference them as such in your scripts. Also, make sure you are talking about each item’s applescript name and not it’s title. Click on an item in IB and then select “Tools > Show Info” (cmd-shft-i). Then select “Applescript” (cmd-7) from the popup button in the info window. The window’s title and AS name are different, and this is an important distinction.

When you use a repeat loop, one of the standard syntax forms is repeating through a list of items, automatically evaluating each item as it is encountered. For example, in the case of this repeat loop…

…the loop repeats for every item in the list ‘myList’, and places the contents of the current list item into the variable ‘tempItem’. Assuming the sample code, on the first loop of the repeat the contents of tempItem would be “Item 1” (the contents of the first item of myList). On the second loop, the contents of tempItem would be “Item 2”… and this would continue through all the items in the list.

How and when you populate your list is up to you. You could do it on ‘launch’ of the app, on ‘will open’ of the window, at the press of a button, etc. I will place the code which populates the button into a subroutine so you can use it anywhere, and create a few connections to it so you can see how it’s used. I changed a few variables in the code to better reflect changes relavent to your setup. See the comments in the code for more content-specific info.

--> Attach this to the 'choose menu item' handler of the popup button
--> This will receive the selection of a menu item by it's name
on choose menu item theObject
	set theDisk to (name of current menu item of theObject)
	display dialog theDisk --> For testing only
	--> insert your shell script code here
end choose menu item

--> Attached to a button named "populate" that updates the current list of disks
on clicked theObject
	if name of theObject is "populate" then
		populatePopup()
	end if
end clicked

--> Attached to the window named "Window"...
--> ...which assumably contains a popup button named "popupButton" that lists the disks
--> Populates the popup button every time the window opens
on will open theObject
	if name of theObject is "Window" then
		populatePopup()
	end if
end will open

--> A subroutine which can be called from any handler
--> Immediately updates the contents of the popup button with the current disks
to populatePopup()
	set excludeDisks to {"BootCD", "Network"}
	tell application "Finder" to set dlist to (name of every disk whose name is not in excludeDisks) as list
	
	tell menu of popup button "popupButton" of window "Window"
		try --> Try to delete any existing menu items
			delete every menu item
		end try

		repeat with tempd in dlist
			make new menu item at end of menu items with properties {name:tempd, title:tempd, enabled:true}
		end repeat
	end tell
end populatePopup

Hopefully, I have made this easier to understand and implement, and not more complicated. :smiley:

Take care…
j

It worked great. Thank you for taking the time to explain it to me. I will post my project when I am finished.

Thank you,

Damon

Hello Jobu,

Do you know how to make a list from a shell script? I am unable to use the Finder to generate the disk list because it has been stripped out of the boot cd. When I use the script below the popup displays each character on a separate line.

Any clue?

Thank you,

Damon

to populatePopuptarget()
	set excludeDisks to {"BootCD", "Network"}
	set tlist to do shell script "ls /volumes"
	tell menu of popup button "targetButton" of window "Window"
		try --> Try to delete any existing menu items 
			delete every menu item
		end try
		
		repeat with tempd in tlist
			make new menu item at end of menu items with properties {name:tempd, title:tempd, enabled:true}
		end repeat
	end tell
end populatePopuptarget

Try adding this line right after the do shell script line…

set tlist to (paragraphs of tlist) as list

j

Thank you again Jobu.

I was trying to make it a list using

 set tlist2 to {tlist}

Then using tlist2 as the list but that put everything on one line. I am assuming that the (paragraphs of tlist) took care of the formatting.

Thank you,

Damon

I will be bugging you next week about how to get status from the terminal into a ProgressIndicator.

Terminal uses …10…20…