force a variable into a handler

i am just starting to write handlers

and am unsure how to force a variable that is defined in the body of a script into the handler

ie


--=========== Use this set to control rotation globally===========

display dialog "What angle would you like for files?" & return & "use - for counter clockwise" default answer "-90"
set the_angle to the text returned of the result


--======== end global control ===============


on open these_items
	repeat with i from 1 to the count of these_items
		set this_item to (item i of these_items)
		set the item_info to info for this_item
		if folder of the item_info is true then
			process_folder(this_item)
		else if (alias of the item_info is false) then
			my process_item(this_item)
		end if
	end repeat
end open

-- this sub-routine processes folders
on process_folder(this_folder)
	set these_items to list folder this_folder without invisibles
	repeat with i from 1 to the count of these_items
		set this_item to alias ((this_folder as text) & (item i of these_items))
		set the item_info to info for this_item
		if folder of the item_info is true then
			process_folder(this_item)
		else if (alias of the item_info is false) then
			my process_item(this_item)
		end if
	end repeat
end process_folder

-- this sub-routine rotates the files
on process_item(this_item)
	(*
	--============ use these to control each file's angle individually=========
	set the_info to info for this_item
	display dialog "What angle would you like for file" & return & ((name of the_info) as text) & return & "use - for counter clockwise" default answer "-90"
	set the_angle to the text returned of the result
	
	--==========  end individual angle control=============
	*)
	set the_file to this_item
	tell application "Image Capture Scripting"
		set this_image to open the_file
		rotate this_image to angle (the_angle as number)
		save this_image in the_file
		close this_image
	end tell
end process_item


the_angle is undefined

do i have to do this

on process item(this_item, the_angle) …

You can do that or make the variable global.

global the_angle

I prefer to pass the variable to the handler but this is simply because I’m a slacker when it comes to learning about the scope of variables. :stuck_out_tongue:

– Rob

Where do i put the Global the_angle

beginning of script?
begin of handler?

=========== Use this set to control rotation globally===========

display dialog “What angle would you like for files?” & return & “use - for counter clockwise” default answer “-90”
set the_angle to the text returned of the result

global the_angle

======== end global control ===============

on open these_items
repeat with i from 1 to the count of these_items
set this_item to (item i of these_items)
set the item_info to info for this_item
if folder of the item_info is true then
[color=green]process_folder/color
else if (alias of the item_info is false) then
my [color=green]process_item/color
end if
end repeat
end open

this sub-routine processes folders
on [color=green]process_folder/color
set these_items to list folder this_folder without invisibles
repeat with i from 1 to the count of these_items
set this_item to alias ((this_folder as text) & (item i of these_items))
set the item_info to info for this_item
if folder of the item_info is true then
[color=green]process_folder/color
else if (alias of the item_info is false) then
my [color=green]process_item/color
end if
end repeat
end process_folder

this sub-routine rotates the files
on [color=green]process_item/color
global the_angle
(*[color=olive]
–============ use these to control each file’s angle individually=========
set the_info to info for this_item
display dialog “What angle would you like for file” & return & ((name of the_info) as text) & return & “use - for counter clockwise” default answer “-90”
set the_angle to the text returned of the result

 --==========  end individual angle control=============
 [/color]*)
 [color=blue]set[/color] [color=green]the_file[/color] [color=blue]to[/color] [color=green]this_item[/color]
 [color=blue]tell[/color] [color=blue]application[/color] "Image Capture Scripting"
      [color=blue]set[/color] [color=green]this_image[/color] [color=blue]to[/color] [color=blue]open[/color] [color=green]the_file[/color]
      [color=blue]rotate[/color] [color=green]this_image[/color] [color=blue]to angle[/color] ([color=green]the_angle[/color] [color=blue]as[/color] [color=blue]number[/color])
      [color=blue]save[/color] [color=green]this_image[/color] [color=blue]in[/color] [color=green]the_file[/color]
      [color=blue]close[/color] [color=green]this_image[/color]
 [color=blue]end[/color] [color=blue]tell[/color]

end process_item

this script was automatically tagged for
color coded syntax by Script to Markup Code
written by Jonathan Nathan

I generally put them at the beginning of the script.

You can experiment by moving the global declaration to different locations in the following code.

global foo
set foo to "hello"
test()

on test()
	display dialog foo
end test

– Rob

Am i missing something cause it still errors out and tells me the variable the_angle is not defined

code is here

on run
set thefolder to (choose folder) as alias
set these_items to list folder thefolder without invisibles
open thefolder & “:” & these_items
end run

on open these_items
global the_angle
=========== Use this set to control rotation globally===========

 [color=blue]display dialog[/color] "What angle would you like for files?" & [color=blue]return[/color] & "use - for counter clockwise" [color=blue]default answer[/color] "-90"
 [color=blue]set[/color] [color=green]the_angle[/color] [color=blue]to[/color] [color=blue]the[/color] [color=blue]text returned[/color] [color=blue]of[/color] [color=blue]the[/color] [color=blue]result[/color]
 
 
 
 --[color=olive]======== end global control ===============[/color]
 
 
 [color=blue]repeat[/color] [color=blue]with[/color] [color=green]i[/color] [color=blue]from[/color] 1 [color=blue]to[/color] [color=blue]the[/color] [color=blue]count[/color] [color=blue]of[/color] [color=green]these_items[/color]
      [color=blue]set[/color] [color=green]this_item[/color] [color=blue]to[/color] ([color=blue]item[/color] [color=green]i[/color] [color=blue]of[/color] [color=green]these_items[/color])
      [color=blue]set[/color] [color=blue]the[/color] [color=green]item_info[/color] [color=blue]to[/color] [color=blue]info for[/color] [color=green]this_item[/color]
      [color=blue]if[/color] [color=blue]folder[/color] [color=blue]of[/color] [color=blue]the[/color] [color=green]item_info[/color] [color=blue]is[/color] [color=blue]true[/color] [color=blue]then[/color]
           [color=green]process_folder[/color]([color=green]this_item[/color])
      [color=blue]else[/color] [color=blue]if[/color] ([color=blue]alias[/color] [color=blue]of[/color] [color=blue]the[/color] [color=green]item_info[/color] [color=blue]is[/color] [color=blue]false[/color]) [color=blue]then[/color]
           [color=blue]my[/color] [color=green]process_item[/color]([color=green]this_item[/color])
      [color=blue]end[/color] [color=blue]if[/color]
 [color=blue]end[/color] [color=blue]repeat[/color]

end open

this sub-routine processes folders
on [color=green]process_folder/color
set these_items to list folder this_folder without invisibles
repeat with i from 1 to the count of these_items
set this_item to alias ((this_folder as text) & (item i of these_items))
set the item_info to info for this_item
if folder of the item_info is true then
[color=green]process_folder/color
else if (alias of the item_info is false) then
my [color=green]process_item/color
end if
end repeat
end process_folder

this sub-routine rotates the files
on [color=green]process_item/color

 (*[color=olive]
 --============ use these to control each file's angle individually=========
 set the_info to info for this_item
 display dialog "What angle would you like for file" & return & ((name of the_info) as text) & return & "use - for counter clockwise" default answer "-90"
 set the_angle to the text returned of the result
 
 --==========  end individual angle control=============
 [/color]*)
 [color=blue]set[/color] [color=green]the_file[/color] [color=blue]to[/color] [color=green]this_item[/color]
 [color=blue]tell[/color] [color=blue]application[/color] "Image Capture Scripting"
      [color=blue]set[/color] [color=green]this_image[/color] [color=blue]to[/color] [color=blue]open[/color] [color=green]the_file[/color]
      [color=blue]rotate[/color] [color=green]this_image[/color] [color=blue]to angle[/color] ([color=red]the_angle[/color] [color=blue]as[/color] [color=blue]number[/color])
      [color=blue]save[/color] [color=green]this_image[/color] [color=blue]in[/color] [color=green]the_file[/color]
      [color=blue]close[/color] [color=green]this_image[/color]
 [color=blue]end[/color] [color=blue]tell[/color]

end process_item

this script was automatically tagged for
color coded syntax by Script to Markup Code
written by Jonathan Nathan

Does this work?

global the_angle

on run
	set thefolder to (choose folder) as alias
	set these_items to list folder thefolder without invisibles
	open thefolder & ":" & these_items
end run

on open these_items
	-- =========== Use this set to control rotation globally=========== 
	
	display dialog "What angle would you like for files?" & return & "use - for counter clockwise" default answer "-90"
	set the_angle to the text returned of the result
	
	
	
	-- ======== end global control =============== 
	
	
	repeat with i from 1 to the count of these_items
		set this_item to (item i of these_items)
		set the item_info to info for this_item
		if folder of the item_info is true then
			process_folder(this_item)
		else if (alias of the item_info is false) then
			my process_item(this_item)
		end if
	end repeat
end open

-- this sub-routine processes folders 
on process_folder(this_folder)
	set these_items to list folder this_folder without invisibles
	repeat with i from 1 to the count of these_items
		set this_item to alias ((this_folder as text) & (item i of these_items))
		set the item_info to info for this_item
		if folder of the item_info is true then
			process_folder(this_item)
		else if (alias of the item_info is false) then
			my process_item(this_item)
		end if
	end repeat
end process_folder

-- this sub-routine rotates the files 
on process_item(this_item)
	
	(* 
--============ use these to control each file's angle individually========= 
set the_info to info for this_item 
display dialog "What angle would you like for file" & return & ((name of the_info) as text) & return & "use - for counter clockwise" default answer "-90" 
set the_angle to the text returned of the result 

--==========  end individual angle control============= 
*)
	set the_file to this_item
	tell application "Image Capture Scripting"
		set this_image to open the_file
		rotate this_image to angle (the_angle as number)
		save this_image in the_file
		close this_image
	end tell
end process_item

– Rob

yes and no

it went through the items then the end i get this error

pasted below is in the events log window

tell current application
info for “:”
{name:“MacOS”, creation date:date “Saturday, September 27, 2003 2:23:06 AM”, modification date:date “Saturday, September 27, 2003 2:23:06 AM”, icon position:{0, 0}, size:3.77748E+5, folder:true, alias:false, name extension:missing value, extension hidden:false, visible:true, package folder:false, file type:"file creator:"displayed name:“MacOS”, default application:alias “Macintosh HD:System:Library:CoreServices:Finder.app:”, kind:“Folder”, folder window:{0, 0, 0, 0}}
list folder “:” without invisibles
{“Script Editor”}
“File :Script Editor wasn’t found.”

I’m not sure what to make of the error at this point. In your run handler, what is this line supposed to do? Maybe it’s obvious and maybe I’m suffering another dense moment but I don’t get it. :stuck_out_tongue:

open thefolder & ":" & these_items

On another note regarding the run handler, it isn’t necessary to add “as alias” to ‘set thefolder to (choose folder)’ since it will return an alias anyway.

– Rob

when i take of the & “:” & these_items

it errors this:
“Can’t get item 1 of alias “Macintosh HD:Users:jbradfield:Desktop:images:”.”

Joe,

I think this might do what you want it to do but it probably wouldn’t hurt to add some error checks.

global the_angle

on run
	set thefolder to (choose folder)
	tell application "Finder" to ¬
		set these_items to items of thefolder as alias list
	process_all(these_items)
end run

on open these_items
	process_all(these_items)
end open

on process_all(items_)
	-- =========== Use this set to control rotation globally=========== 
	
	display dialog "What angle would you like for files?" & return & "use - for counter clockwise" default answer "-90"
	set the_angle to the text returned of the result
	
	-- ======== end global control =============== 
	
	repeat with i from 1 to the count of items_
		set this_item to (item i of items_)
		set the item_info to info for this_item
		if folder of the item_info is true then
			process_folder(this_item)
		else if (alias of the item_info is false) then
			my process_item(this_item)
		end if
	end repeat
end process_all

-- this sub-routine processes folders 
on process_folder(this_folder)
	tell application "Finder" to ¬
		set these_items to items of folder this_folder as alias list
	repeat with i from 1 to the count of these_items
		set this_item to item i of these_items
		set the item_info to info for this_item
		if folder of the item_info is true then
			process_folder(this_item)
		else if (alias of the item_info is false) then
			my process_item(this_item)
		end if
	end repeat
end process_folder

-- this sub-routine rotates the files 
on process_item(this_item)
	
	(* 
--============ use these to control each file's angle individually========= 
set the_info to info for this_item 
display dialog "What angle would you like for file" & return & ((name of the_info) as text) & return & "use - for counter clockwise" default answer "-90" 
set the_angle to the text returned of the result 

--==========  end individual angle control============= 
*)
	
	tell application "Image Capture Scripting"
		set this_image to open this_item
		rotate this_image to angle (the_angle as number)
		save this_image in this_item
		close this_image
	end tell
end process_item

– Rob