on open these_items
repeat with i from 1 to number of items in these_items
set this_item to item i of these_items
set the item_info to the info for this_item
if (alias of the item_info is false and the folder of the item_info is true) then -- it's a folder, and it's not an alias, i suppose :)
set full_item to every file of this_item
open full_item
end if
end repeat
end open
Script says me that it’s impossibile to get every item of alias…
What’s wrong on command
Applescript doesn’t know anything about files of an alias,
but the Finder does:
on open these_items
repeat with this_item in these_items
set {isAlias, isFolder} to {alias, folder} of (info for this_item)
if (isAlias is false and isFolder) then -- it's a folder, and it's not an alias, i suppose :)
tell application "Finder"
set full_item to every file of this_item
open full_item
end tell
end if
end repeat
end open
So, I’ve study those and tried to implementig your string in my Script.
property type_list : {"JPEG", "TIFF", "PNGf", "PICT"}
property extension_list : {"jpg", "jpeg", "tif", "tiff", "png", "pict"}
property these_items : {""}
on open these_items
set_folder(these_items)
process_item(these_items)
end open
on set_folder(these_items)
repeat with this_item in these_items
set {isAlias, isFolder} to {alias, folder} of (info for this_item)
if (isAlias is false and isFolder is true) then
display dialog "E' una cartella!!!"
tell application "Finder"
set these_items to every file of this_item
open these_items
end tell
exit repeat
end if
end repeat
end set_folder
on process_item(these_items)
set esistenza to false
repeat with i from 1 to number of items in these_items
set this_item to item i of these_items
set the item_info to the info for this_item
if file (type of the item_info is in the type_list) or (the name extension of the item_info is in the extension_list) then
set esistenza to true
end if
end repeat
if esistenza is true then
-- do something
else
display dialog "Only Jpeg/Png/Tiff/Pict" buttons {"OK"} default button 1 with icon 0
end if
end process_item
So, in the handler “set_folder(these_items)”
I ceck all files in these_items variable. If one of them is a folder, then script try to put all files inside this folder to these_items variable.
In the end, “these_items” is a group of files.
Right? No. The “set_folder(these_items)” handler seems to run fine, but there is something that do not work fine in the “process_item(these_items)” handler, or something wrong in these_items variables.
on process_item(these_items)
set esistenza to false
repeat with this_item in these_items
tell (info for this_item) to set {fType, fExt} to {file type, name extension}
if fType is in type_list or fExt is in extension_list then
set esistenza to true
-- do something
else
display dialog "Only Jpeg/Png/Tiff/Pict" buttons {"OK"} default button 1 with icon 0
end if
end repeat
end process_item
Stefans AS-Fu is stronger than mine, but here is my take…
The problem, I think, is that your property these_items is independant of your variables these_items. Properties are supposed to retain their state across runs, but run this
property these_items : {""}
on open these_items
set x to these_items
display dialog "Showing you the dropped these items: " & x
end open
display dialog "Showing you the property these items: " & these_items
And you can that the on open these_Items variable is different that the property these_items. If you drop something you get the proper display, if just run it then you still see a “” afterwards.
Now try it like this
property prop_these_items : {""}
on open these_items
set x to these_items
set prop_these_items to x
display dialog "Showing you the dropped these items: " & x
end open
display dialog "Showing you the property these items: " & prop_these_items
And you see the expected behavior. So in your script your property is never getting updated since its name coincides with a local variable and your never returning a variable back out of one handler to be used in another.
on open these_items
set_folder(these_items)
process_item(these_items)
end open
Now we all know that variables within a handler are local… It appeared that to me within in set_folder he was trying to dig files from a folder and process those files in the process_item handler. In the set folder handler he manipulates the content of the these_items variable which he then passes to the next handler, but those two variables have different scope… so since he’s not returning the contents in set_folder he’s not passing the proper contents to process_item.
That or I am totally off base on my thinking and with understanding what is trying to be accomplished here =)
I’ve ceked the script on x-ray. Now I am the Master of this script! Only of this
Error in Property is a good idea. I have a lot of problem with global variables.
So, I’ve changed the scrtpt in this mode
property type_list : {"JPEG", "TIFF", "PNGf", "PICT"}
property extension_list : {"jpg", "jpeg", "tif", "tiff", "png", "pict"}
global these_items
on open these_items
set_folder(these_items)
display dialog "3 these_items =" & (these_items as string) -- dialog 3
process_item(these_items)
end open
on run
set these_items to choose file with prompt "Select file" with multiple selections allowed without invisibles
process_item(these_items)
end run
on set_folder(these_items)
repeat with this_item in these_items
set {isAlias, isFolder} to {alias, folder} of (info for this_item)
if (isAlias is false and isFolder is true) then
tell application "Finder"
set these_items to every file of this_item
display dialog "1 this_item = " & (this_item as string) -- dialog 1
display dialog "2 these_items = " & (these_items as string) -- dialog 2
open these_items
end tell
exit repeat
end if
end repeat
end set_folder
on process_item(these_items)
set esistenza to false
repeat with this_item in these_items
tell (info for this_item) to set {fType, fExt} to {file type, name extension}
if fType is in type_list or fExt is in extension_list then
set esistenza to true
else
set esistenza to false
end if
end repeat
So, if I drag a folder with 2 jpeg files.
In the dialog 1 the box tell me the path of the folder in hfs format (this_item). So, it means that this_item is a folder!
After there is a command
set these_items to every file of this_item
So, here, these_items would be a list (yes, not a group) of files, but the dialog 2 tell me an empty dialog box.
In the end, in the dialog 3, var “these_items”, that are global as I order in the beginning of the script, return to the initial value (a folder). Why???
Now I got what you want!!
Please do not use the same names for properties/globals and handler variables like on open xxxx
You must return explicitly the list of the files ( I use an extra variable my_items)
Local variables in different scopes is no problem
property type_list : {"JPEG", "TIFF", "PNGf", "PICT"}
property extension_list : {"jpg", "jpeg", "tif", "tiff", "png", "pict"}
on open these_items
set my_items to set_folder(these_items)
process_item(my_items)
end open
on run
set these_items to choose file with prompt "Select file" with multiple selections allowed without invisibles
process_item(these_items)
end run
on set_folder(these_items)
repeat with this_item in these_items
set {isAlias, isFolder} to {alias, folder} of (info for this_item)
if (isAlias is false and isFolder is true) then
tell application "Finder"
set my_items to every file of this_item
open my_items
end tell
return my_items
end if
end repeat
end set_folder
on process_item(these_items)
set esistenza to false
repeat with this_item in these_items
tell (info for this_item) to set {fType, fExt} to {file type, name extension}
if fType is in type_list or fExt is in extension_list then
set esistenza to true
else
set esistenza to false
end if
end repeat
end process_item
Oh, I want a lot of thinks! Money, Cars, no animals in home, time, a new Mac… another, another another…
(…)
Oh, you talking about the script
Yes, this is what I want =)
I’ve read and understand what you mean.
But I have to read and understand a much more about local and global variables, isn’t it?
You using “return” in the handler and “set to” in the "on open handler. THis run fine, but not so easy for me.
on set_folder(these_items)
repeat with this_item in these_items
set {isAlias, isFolder} to {alias, folder} of (info for this_item)
if (isAlias is false and isFolder is true) then
tell application "Finder"
set my_items to every file of this_item
open my_items
end tell
return my_items
end if
end repeat
end set_folder
Is if more than one folder is dropped it will only process the files of the first folder since the return kicks you out of the repeat.
Right! What’s about to call the second handler within the first?
...
on open these_items
set_folder(these_items)
end open
on run
set these_items to choose file with prompt "Select file" with multiple selections allowed without invisibles
process_item(these_items)
end run
on set_folder(these_items)
repeat with this_item in these_items
set {isAlias, isFolder} to {alias, folder} of (info for this_item)
if (isAlias is false and isFolder is true) then
tell application "Finder"
set my_items to every file of this_item
open my_items
end tell
process_item(my_items)
end if
end repeat
end set_folder
on process_item(these_items)
set esistenza to false
repeat with this_item in these_items
tell (info for this_item as alias) to set {fType, fExt} to {file type, name extension}
if fType is in type_list or fExt is in extension_list then
set esistenza to true
else
set esistenza to false
end if
end repeat
end process_item
It’s quite easy:
Each handler (also the on run handler) is like a closed house.
Every variable defined within a handler is invisible for other handlers,
i.e. the variable is valid only in the handler, where it’s defined.
A property or a global declared variable makes the walls of the closed hous(es) transparent,
therefore the value is valid in all handlers.
There is only one way to bring things in the house and only one way out:
In: the parameter parentheses of the handler Out: The return statement (if no return statment is used, the last result will be returned)
To return multiple values use a list like return {a, b}
property type_list : {"JPEG", "TIFF", "PNGf", "PICT"}
property extension_list : {"jpg", "jpeg", "tif", "tiff", "png", "pict"}
on open these_items
set_folder(these_items)
end open
on run
set these_items to choose file with prompt "Select file" with multiple selections allowed without invisibles
process_item(these_items)
end run
on set_folder(these_items)
set theFiles to {}
repeat with this_item in these_items
set {isAlias, isFolder} to {alias, folder} of (info for this_item)
if (isAlias is false and isFolder is true) then
tell application "Finder"
set my_items to every file of this_item
-- open my_items
end tell
process_item(my_items)
else
set end of theFiles to this_item as alias
end if
end repeat
process_item(theFiles)
end set_folder
on process_item(these_items)
set esistenza to false
repeat with this_item in these_items
tell (info for this_item) to set {fType, fExt} to {file type, name extension}
if fType is in type_list or fExt is in extension_list then
set esistenza to true
else
set esistenza to false
end if
end repeat
end process_item
For the first time, Stefank, one of your script give me an error! So, this means that you are human!
=)
I’ve tried to drag a folder that contains “Ambient 46.jpg” and “Ambient 47.jpg”. The error says “Can’t find document «class docf» Ambient 46.jpg” (message are in Italian, this is my translate).
In the end, I’ve read everything in the script but what this row?
I quote myself
I’ve undestand: theFiles is a list of file (first the list is empty), every repeat, file of this_item goes in the end of (after last file of) theFiles. Right?
Now, error is here
set my_items to every file of this_item
Finder know elements this_item, but apparently can’t operate with “every file of”.