Hello, I am just starting to dig into Applescript with Hanaan Rosenthal’s book and am having some trouble with my first app. I am trying to make an app that changes the colour of labels of all the folders within a folder. Early on in the script, I get an error saying “The variable the_colour is not defined.” Isn’t it defined in the if statement? Any help would be greatly appreciated.
Pete
--This is a portion of a script that changes the labels of folders
--Why do I get an error saying "The variable the_colour is not defined."?
--Isn't it defined in the if statement?
set the_folder to (choose folder)
choose from list {"unlabeled", "red"}
if the result is "unlabeled" then
set the_colour to 0
else if the result is "red" then
set the_colour to 1
end if
get the_colour
(*
change_label_to_colour(the_folder)
on change_label_to_colour(the_folder)
tell application "Finder"
--Check each of the alias files in this disk/folder
set label index of the_folder to the_colour
--Clean the alias files in each sub-folder
set sub_folders_list to folders of the_folder
repeat with the_sub_folder_ref in sub_folders_list
my change_label_to_colour(the_sub_folder_ref)
end repeat
end tell
end change_label_to_colour
*)
choose from list returns a list (or false if the dialog was canceled), and a list is not equal to a string. Try something like this:
choose from list {"unlabeled", "red"}
set the_colour to result
if the_colour is false then error number -128 -- User canceled
set the_colour to first item of the_colour
if the result is "unlabeled" then
set the_colour to 0
else if the result is "red" then
set the_colour to 1
end if
Hi Thanks for your response. OK so I’m getting there. The first part of the script works but the variable the_colour gets an ‘undefined’ error in the second half of the script.
hmmm.
--This script changes the label of a folder to orange recursively
choose from list {"unlabeled", "red"}
set the_colour to result
if the_colour is false then error number -128 -- User canceled
set the_colour to first item of the_colour
if the result is "unlabeled" then
set the_colour to 0
else if the result is "red" then
set the_colour to 1
end if
set the_folder to (choose folder)
change_label_to_orange(the_folder)
on change_label_to_orange(the_folder)
tell application "Finder"
--Check each of the alias files in this disk/folder
set label index of the_folder to the_colour
--Clean the alias files in each sub-folder
set sub_folders_list to folders of the_folder
repeat with the_sub_folder_ref in sub_folders_list
my change_label_to_orange(the_sub_folder_ref)
end repeat
end tell
end change_label_to_orange
variables are local by default, that means, they will only be recognized in the same scope.
Each handler has it’s own scope, so the handler change_label_to_orange() has no idea about the variable the_colour.
Either you can define the variable global with
global the_colour
this can be done at the beginning of the script or at the beginning in the handler
or use a property
property the_colour : ""
properties must be defined at top level (unless it’s a script object)
The problem you are running into is one of variable scoping. Check out the AppleScript Language Guide section on Scope of Properties and Variables Declared at the Top Level of a Script (also the Run Handlers section for why all that top level code is not the same top level you might expect from experience with other programming languages). You can make your script work by declaring your variable to be global:
global the_colour
-- rest of your script here, verbatim
Also, if all you want to do is change the label of the folder and its subfolders, you can do it without recursion:
on change_label_to_orange(the_folder)
tell application "Finder"
set label index of the_folder to the_colour
set label index of every folder of entire contents of the_folder to the_colour
end tell
end change_label_to_orange
Hi I was wondering if any one knows why on some computers this portion of the script executes well:
on change_label_to_orange(the_folder)
tell application "Finder"
set label index of the_folder to the_colour
set label index of every folder of entire contents of the_folder to the_colour
end tell
end change_label_to_orange
While on other similarly configured computers it won’t work unless it is written like this:
on change_label_to_orange(the_folder)
tell application "Finder"
set label index of folder the_folder to the_colour
set label index of every folder of entire contents of folder the_folder to the_colour
end tell
end change_label_to_orange
With the first script I sometimes get an error saying “can’t set <>”
I got it working, I was just wondering why I only have to include “folder” on some computers.
Hmmm
Pete