Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to differentiate a folder from a file

7 views
Skip to first unread message

barbosaleonardo1

unread,
Sep 18, 2016, 6:59:40 PM9/18/16
to
Hello all,

I don't know much about applescript, but I do know there's a kind called 'folder'. I would like to write a script that takes an item as input and, if the item is a folder, set the current Finder window to this folder. However, if the folder is file, then the script should just open the file.

I will appreciate any help on how I could come up with the best way of doing that. Pointers to hands-on introduction material to apple-scripting is also welcome.

BR

L

Jolly Roger

unread,
Sep 19, 2016, 1:17:02 PM9/19/16
to
1. Open /Applications/Utilities/Script Editor.
2. Choose File > Open Dictionary from the menu bar.
3. Open the "Standard Additions" dictionary.

Standard Additions provides a lot of functionality to any script. In
your case, you want information about the "info for" command. You can
search for "info" in the search box at the top-right corner of the
dictionary window to view the command. This is what it looks like:

info for v : Return information for a file or folder
info for file : an alias or file reference to the file or folder
[size boolean] : Return the size of the file or folder?
(default is true)
→ file information : a record containing the information
for the specified file or folder

And if you click the "file information" link, you'll get a description
of the "file information" record, which looks like this:

file information n : Reply record for the ‘info for’ command
PROPERTIES
name (text, r/o) : the name of the item
displayed name (text, r/o) : the user-visible name of the item
short name (text, r/o) : the short name (CFBundleName) of the item
(if the item is an application)
name extension (text, r/o) : the name extension of the item (such
as “txt”)
bundle identifier (text, r/o) : the item’s bundle identifier (if
the item is a package)
type identifier (text, r/o) : the item’s type identifier
kind (text, r/o) : the kind of the item
default application (alias, r/o) : the application that normally
opens this kind of item
creation date (date, r/o) : the date the item was created
modification date (date, r/o) : the date the item was last modified
file type (text, r/o) : the file type of the item
file creator (text, r/o) : the creator type of the item
short version (text, r/o) : the item’s short version string (from
the Finder’s ‘Get Info’ box)
long version (text, r/o) : the item’s long version string (from the
Finder’s ‘Get Info’ box)
size (integer, r/o) : the size of the item in bytes
alias (boolean, r/o) : Is the item an alias file?
folder (boolean, r/o) : Is the item a folder?
.
.
.
...and so on

Notice that the last line above is a "folder" property that has a
boolean value (true or false) indicating whether the item is a folder or
not. That's the property you want.

So if you have the item in question in a variable (let's call it
nextItem), then you could get the info for that item with something like
"info for nextItem", and then you could get the folder property with
something like "folder of (info for nextItem)".

Here's how you could access the property in a droplet script:

-- begin script
on open itemList
repeat with nextItem in itemList
set isFolder to folder of (info for nextItem)

if isFolder then
set myMessage to "This is a folder:" & return & return & the POSIX path of nextItem
else
set myMessage to "This is NOT a folder:" & return & return & the POSIX path of nextItem
end if

activate
display alert "Folder Tester" message myMessage buttons {"Thanks"} default button 1
end repeat
end open
-- end script

Note: Usenet clients sometimes introduce hard line wraps. You may need
to remove the hard line wraps from this script after you copy and paste
it into Script Editor.

1. Enter the above script in a new script in Script Editor.
2. Save the new script as an Application.
3. Drag files or folders and drop them onto the saved application's icon.

The application will examine the item you dropped onto it and display an
alert indicating whether the item is a file or folder.

Make sense?

--
E-mail sent to this address may be devoured by my ravenous SPAM filter.
I often ignore posts from Google. Use a real news client instead.

JR
0 new messages