AppleScript studio and global variables

Is there such a thing?

I’m creating an app where the users places images into a matrix from a folder they select by clicking a button. I then want to be able to click on the image in the matrix and figure out which file is loaded in there. i can figure out the current cell of the matrix through a mouse click and from that I should be able to refer back to the list of files that was generated from the user selecting a folder and get which file it is. Problem is the list of files is in one script (the action for the button to select a folder) and I need to get that into the other script that is the action for clicking on the image.

I was hoping there would be a global variable that would correspond to the entire project but when I put this into my script:

set global folderList to list folder thisFolder without invisibles

my script wouldn’t compile. Are global variables not allowed in AS-S?

You’re not declaring the global correctly. From the documentation:

But this at the top of your script file, then access/use the variable like you normally would.

global folderList

Hi Bruce

Thanks for the reply. I tried as you suggested but when I did I got an error message (folderList not defined).

Here’s the first code, it’s placed on a button which when clicked let’s you select which folder to import the images from:

global folderList

on clicked theObject
	--set variables
	set imageList to {}
	set fileTypeList to {"Camera Raw", "TIFF", "EPSF", "JPEG", "GIF", "msng", "missing value"}
	--set shortcut definitions for our relevant objects
	set photoMatrix to matrix "photoMatrix" of scroll view "scrollbars" of window "addWin"
	--set buttonMatrix to matrix "buttonMatrix" of scroll view "scrollBars" of window "mainWin"
	--pick a folder
	set thisFolder to choose folder
	--get the number of files in the folder
	set folderList to list folder thisFolder without invisibles
	--loop through the list and create a second array of good files
	repeat with thisItem in folderList
		set thisItemPath to (thisFolder as string) & thisItem
		tell application "Finder"
			set fileType to kind of (thisItemPath as alias)
			--display dialog "::" & fileType & "::"
			if fileTypeList contains fileType then
				copy thisItemPath to end of imageList
			end if
		end tell
	end repeat
	-- add rows to matrix
	set imageCount to count of every item of imageList
	set rowCount to round (imageCount / 5) rounding down
		call method "renewRows:columns:" of photoMatrix with parameters {rowCount, 5}
		call method "sizeToCells" of photoMatrix
		--call method "renewRows:columns:" of buttonMatrix with parameters {rowCount, 5}
		--call method "sizeToCells" of buttonMatrix
		update window "addWin"
	on error errMsg
		display dialog "oops! - create rows" & return
	end try
	--import images
	repeat with i from 1 to count of every item of imageList
		set thisImage to item i of imageList as alias
		tell photoMatrix
				set image of cell i of photoMatrix to load image thisImage
			on error errMsg
				display dialog "oops! - import images" & return
			end try
		end tell
	end repeat
end clicked

This is the second script. It is attached to the matrix of images that has been created. When you click on one of the images I am just trying to return what the corresponding item in the variable folderList would be. I keep getting an error message saying folderList is not defined.

on clicked theObject
	--make shortcut to object
	set photoMatrixID to matrix "photoMatrix" of scroll view "scrollbars" of window "addWin"
	--get which cell is being clicked
	set curRow to current row of photoMatrixID
	set curCol to current column of photoMatrixID
	set colCount to (call method "numberOfColumns" of photoMatrixID)
	set curCell to (colCount * (curRow - 1)) + curCol
	log curRow & curCol & colCount & curCell

	--display the corresponding item in the list folderList
	display dialog item curCell of folderList
end clicked

I get an error message saying folderList is not defined.

Is that in a seperate script file? It won’t work then. (You’d have to do something diffrerently.)

Right, that’s what I figured.

What do you think of this - when the user selects a folder writing the path of that folder to a hidden text field. Then when the user clicks an image, pull the path from the text field and reread the folder’s contents and then get the file selected by reading that list.

It seems like a stupid hoop to jump through to do something that should have been packaged with the beta version of a development app. Does anyone know if Apple is even considering adding global variables anytime soon?