Passing Variables

Hey there, Really new, and dont really know much about applescript.

I am piecing together some stuff to automate some repetitive things that i do. i am having trouble running a script as it says that it cannot make the file into a type file…

Here it is…

MAIN SCRIPT

global mmNumber
--parse xml
set theFinalValues to {}
set XMLfile to "Macintosh HD:Users:Admin:Desktop:jobs.xml"
tell application "System Events"
	tell XML element "Main" of contents of XML file XMLfile
		repeat with thisOrder from 1 to (count of XML elements)
			set orderID to (value of XML attribute of XML element thisOrder) as string
			set artistName to (value of (XML elements whose name is "artist") of XML element thisOrder) as string
			set orderNumber to (value of (XML elements whose name is "orderNumber") of XML element thisOrder) as string
			set mmNumber to (value of (XML elements whose name is "mmNumber") of XML element thisOrder) as string
			set nameText to (value of (XML elements whose name is "name") of XML element thisOrder) as string
			set actualWidth to (value of (XML elements whose name is "muralWidth") of XML element thisOrder) as string
			set actualHeight to (value of (XML elements whose name is "muralHeight") of XML element thisOrder) as string
			set crop to (value of (XML elements whose name is "crop") of XML element thisOrder) as string
			set crop1_x to (value of (XML elements whose name is "crop1_x") of XML element thisOrder) as string
			set crop1_y to (value of (XML elements whose name is "crop1_y") of XML element thisOrder) as string
			set crop2_x to (value of (XML elements whose name is "crop2_x") of XML element thisOrder) as string
			set crop2_y to (value of (XML elements whose name is "crop2_y") of XML element thisOrder) as string
			set crop3_x to (value of (XML elements whose name is "crop3_x") of XML element thisOrder) as string
			set crop3_y to (value of (XML elements whose name is "crop3_y") of XML element thisOrder) as string
			set crop4_x to (value of (XML elements whose name is "crop4_x") of XML element thisOrder) as string
			set crop4_y to (value of (XML elements whose name is "crop4_y") of XML element thisOrder) as string
			set theFinalValues to theFinalValues & {{orderID, artistName, orderNumber, mmNumber, nameText, actualWidth, actualHeight, crop, crop1_x, crop1_y, crop2_x, crop2_y, crop3_x, crop3_y, crop4_x, crop4_y}}
			
			repeat with thisData from 1 to count theFinalValues
				set orderNumber to item 3 of item thisData of theFinalValues
				set artistName to item 2 of item thisData of theFinalValues
				set mmNumber to item 4 of item thisData of theFinalValues
				set nameText to item 5 of item thisData of theFinalValues
				set actualWidth to item 6 of item thisData of theFinalValues
				set actualHeight to item 7 of item thisData of theFinalValues
				set crop to item 8 of item thisData of theFinalValues
				set crop1_x to item 9 of item thisData of theFinalValues
				set crop1_y to item 10 of item thisData of theFinalValues
				set crop2_x to item 11 of item thisData of theFinalValues
				set crop2_y to item 12 of item thisData of theFinalValues
				set crop3_x to item 13 of item thisData of theFinalValues
				set crop3_y to item 14 of item thisData of theFinalValues
				set crop4_x to item 15 of item thisData of theFinalValues
				set crop4_y to item 16 of item thisData of theFinalValues
				set theFinalSearchValue to {(item 1 of item thisData of theFinalValues), artistName, orderNumber, mmNumber, nameText, actualWidth, actualHeight, crop, crop1_x, crop1_y, crop2_x, crop2_y, crop3_x, crop3_y, crop4_x, crop4_y}
			end repeat
			
			set scriptPath to "Users:admin:Desktop:Scripting:Scripts:findMasterFile.scpt"
			set x to load script file scriptPath
			run x

then the code goes on to compete more steps…

This is the findMasterFile.scpt file

global mmNumber

set targetFolder to "/Users/Admin/Desktop/"

property the_path : "/Volumes/MYW LIBRARY/MASTER FILES"

set theFiles to findFiles(mmNumber, the_path)
repeat with theFilePath in theFiles
	do shell script "cp " & quoted form of POSIX path of theFiles & " " & quoted form of targetFolder
end repeat
on findFiles(searchName, directoryToSearchIn)
	try
		return every paragraph of (do shell script "mdfind -onlyin " & quoted form of directoryToSearchIn & " -name " & quoted form of searchName)
	on error
		--probably grep didn't found anything
		return {}
	end try
end findFiles

If i paste the second script into the area i am looking to run it, i get errors that its looking for an end or something… not sure what i am doing… :expressionless:

Hello!

If you remove the lines where you run the script, and paste in this instead:


set theFiles to findFiles(mmNumber, "/Volumes/MYW LIBRARY/MASTER FILES")
	repeat with theFilePath in theFiles
		do shell script "cp " & quoted form of POSIX path of theFiles & " " & quoted form of "/Users/Admin/Desktop/"
	end repeat

And then paste in the handler somewhere lower I think you should be good.


on findFiles(searchName, directoryToSearchIn)
		try
			return every paragraph of (do shell script "mdfind -onlyin " & quoted form of directoryToSearchIn & " -name " & quoted form of searchName)
		on error
			--probably grep didn't found anything
			return {}
		end try
	end findFiles

I think the global is the culprit, that the values from your your script is somehow not being passed over, and if they were, then that is due to a bug! :slight_smile:

Now, if you had either set a property of your script, before you ran it, with the value of the mmnumber declared global, that would have worked.

on findFiles(searchName, directoryToSearchIn)
		try
			return every paragraph of (do shell script "mdfind -onlyin " & quoted form of directoryToSearchIn & " -name " & quoted form of searchName)
		on error
			--probably grep didn't found anything
			return {}
		end try
	end findFiles

  set scriptPath to "Users:admin:Desktop:Scripting:Scripts:findMasterFile.scpt"
           set x to load script file scriptPath
           set daNumber of x to mmnumber
           tell x to run 

You could also have declared a run handler in your script and then run the script with parameters.


on run {myparam}
	local targetFolder
	set targetFolder to "/Users/Admin/Desktop/"
	
	local mypath
	set mypath to "/Volumes/MYW LIBRARY/MASTER FILES"
	
	set theFiles to findFiles(myparam, mypath)
	repeat with theFilePath in theFiles
		do shell script "cp " & quoted form of POSIX path of theFiles & " " & quoted form of targetFolder
	end repeat
end run

on findFiles(searchName, directoryToSearchIn)
	try
		return every paragraph of (do shell script "mdfind -onlyin " & quoted form of directoryToSearchIn & " -name " & quoted form of searchName)
	on error
		--probably grep didn't found anything
		return {}
	end try
end findFiles


set scriptPath to "Users:admin:Desktop:Scripting:Scripts:findMasterFile.scpt"
set x to load script file scriptPath
run script x with parameters {mmnumber}

I have just come up with a solution for you that works, should you prefer to have your script object as a separate entity, the two possibilities I have sketched out above.

Dude this is awesome! GREAT thanks so much! i will give this a try.

Hi,

the problem is that the handler returns a list of POSIX paths,
and you cannot get the “quoted form of POSIX path of {list of POSIX path}”.

To pass the list to the shell, the list must be flattened or piped with -exec or -xargs

Hello!

Or use the variable he iterates over in the loop: theFilePath

quoted form of contents of theFilePath should go well, to be totally sure!

Well and maybe the global works after all for passing arguments between scripts! Something to test! :slight_smile:

Hello.

Just for the record, passing values between script objects works just fine. It may not be of the most clean approaches, but I think it works just fine in a secluded area.

The global isn’t saved with the script object that it is called with, I think it is natural that it doesn’t since it has been loaded and run as an instance of something. (Run scripts run a script in a new Applescript environment, so there is no context to be saved when the script has finished running, that is at least how I explain it to myself.)

But, when the script object is running, it is surely sees the global variable in its run handler’s scope.

Now, this is the easiest way for passing values between script objects, though not the most clean approaches. :slight_smile: But should work fine in a specialized area, with out the fuzz of setting and getting properties.

Thanks!

Ok, i have finally gotten a chance to give this a try and i am getting an error

System Events got an error: XML element “Main” of contents of XML file “Macintosh HD:Users:Admin:Desktop:jobs.xml” doesn’t understand the findFiles message.

this is my code now.



set theFinalValues to {}
set XMLfile to "Macintosh HD:Users:Admin:Desktop:jobs.xml"
tell application "System Events"
	tell XML element "Main" of contents of XML file XMLfile
		repeat with thisOrder from 1 to (count of XML elements)
			set orderID to (value of XML attribute of XML element thisOrder) as string
			set artistName to (value of (XML elements whose name is "artist") of XML element thisOrder) as string
			set orderNumber to (value of (XML elements whose name is "orderNumber") of XML element thisOrder) as string
			set mmNumber to (value of (XML elements whose name is "mmNumber") of XML element thisOrder) as string
			set nameText to (value of (XML elements whose name is "name") of XML element thisOrder) as string
			set actualWidth to (value of (XML elements whose name is "muralWidth") of XML element thisOrder) as string
			set actualHeight to (value of (XML elements whose name is "muralHeight") of XML element thisOrder) as string
			set crop to (value of (XML elements whose name is "crop") of XML element thisOrder) as string
			set crop1_x to (value of (XML elements whose name is "crop1_x") of XML element thisOrder) as string
			set crop1_y to (value of (XML elements whose name is "crop1_y") of XML element thisOrder) as string
			set crop2_x to (value of (XML elements whose name is "crop2_x") of XML element thisOrder) as string
			set crop2_y to (value of (XML elements whose name is "crop2_y") of XML element thisOrder) as string
			set crop3_x to (value of (XML elements whose name is "crop3_x") of XML element thisOrder) as string
			set crop3_y to (value of (XML elements whose name is "crop3_y") of XML element thisOrder) as string
			set crop4_x to (value of (XML elements whose name is "crop4_x") of XML element thisOrder) as string
			set crop4_y to (value of (XML elements whose name is "crop4_y") of XML element thisOrder) as string
			set theFinalValues to theFinalValues & {{orderID, artistName, orderNumber, mmNumber, nameText, actualWidth, actualHeight, crop, crop1_x, crop1_y, crop2_x, crop2_y, crop3_x, crop3_y, crop4_x, crop4_y}}
			
			repeat with thisData from 1 to count theFinalValues
				set orderNumber to item 3 of item thisData of theFinalValues
				set artistName to item 2 of item thisData of theFinalValues
				set mmNumber to item 4 of item thisData of theFinalValues
				set nameText to item 5 of item thisData of theFinalValues
				set actualWidth to item 6 of item thisData of theFinalValues
				set actualHeight to item 7 of item thisData of theFinalValues
				set crop to item 8 of item thisData of theFinalValues
				set crop1_x to item 9 of item thisData of theFinalValues
				set crop1_y to item 10 of item thisData of theFinalValues
				set crop2_x to item 11 of item thisData of theFinalValues
				set crop2_y to item 12 of item thisData of theFinalValues
				set crop3_x to item 13 of item thisData of theFinalValues
				set crop3_y to item 14 of item thisData of theFinalValues
				set crop4_x to item 15 of item thisData of theFinalValues
				set crop4_y to item 16 of item thisData of theFinalValues
				set theFinalSearchValue to {(item 1 of item thisData of theFinalValues), artistName, orderNumber, mmNumber, nameText, actualWidth, actualHeight, crop, crop1_x, crop1_y, crop2_x, crop2_y, crop3_x, crop3_y, crop4_x, crop4_y}
			end repeat
			
			set theFiles to findFiles(mmNumber, "/Volumes/MYW LIBRARY/MASTER FILES")
			repeat with theFilePath in theFiles
				do shell script "cp " & quoted form of POSIX path of theFiles & " " & quoted form of "/Users/Admin/Desktop/"
			end repeat
			
			
			
			-- Javascript
			
			tell application "Adobe Photoshop CS6"
				activate
				
				do javascript " "
				
			end tell
			-- end javascript
			
			
		end repeat
	end tell
end tell
on findFiles(searchName, directoryToSearchIn)
	try
		return every paragraph of (do shell script "mdfind -onlyin " & quoted form of directoryToSearchIn & " -name " & quoted form of searchName)
	on error
		--probably grep didn't found anything
		return {}
	end try
end findFiles
-- end xml
 

I’s sorry if i am confusing… i just dont get whats going on really! :slight_smile:

Hello!

What is going on, is that inside a tell block, the application thinks everything there, that is command verbs is something of its own, that it has to resolve. Now your contents of your XML, doesn’t know how to resolve the find files. To tell the contents of the XML block that your script has a handler to do that job, you put my in front of it, like this:


 set theFiles to my findFiles(mmNumber, "/Volumes/MYW LIBRARY/MASTER FILES")

SWEET! it works. thanks SO much… but now i ran into an issue. i have some file names that have apostrophes in them! so now for those files i get an error that says:

error “Can’t make quoted form of «class posx» of {"/Volumes/MYW LIBRARY/MASTER FILES/Barnhouse Dave/MMIADB4002 America’s Healand MF.psd"} into type Unicode text.” number -1700 from quoted form of «class posx» of {“/Volumes/MYW LIBRARY/MASTER FILES/Barnhouse Dave/MMIADB4002 America’s Healand MF.psd”} to Unicode text

Ok, now i checked for a file without the apostrophe and it still doesnt work. i get this same message… i wonder if i should be using a different method? idk.

Hello.

The problems lies within the loop where you call the do shell script with files.

You are sending the whole list to the command every time when you do this:

 repeat with theFilePath in theFiles
               do shell script "cp " & quoted form of POSIX path of theFiles & " " & quoted form of "/Users/Admin/Desktop/"
           end repeat

Try this:

 repeat with theFilePath in theFiles
               do shell script "cp " & quoted form of POSIX path of theFilePath & " " & quoted form of "/Users/Admin/Desktop/"
           end repeat

Just for the record, you can use the results of mdfind (like in find) and send them to xargs so the results are copied immediately.

do shell script "mdfind -0 -onlyin " & quoted form of directoryToSearchIn &" -name " & quoted form of searchName & " | xargs -0 -I {}  cp {} \"$HOME/Dekstop\""

:slight_smile: