Re: Applescript-users Digest, Vol 2, Issue 586
Re: Applescript-users Digest, Vol 2, Issue 586
- Subject: Re: Applescript-users Digest, Vol 2, Issue 586
- From: Cliff Pruitt <email@hidden>
- Date: Tue, 6 Sep 2005 15:58:38 -0400
I'm an AppleScript newby, and this kind of thing really makes me
loose faith in AS. I never felt this frustrated before when learning
any real programming languages :-(
He he... Welcome to learning Applescript... :-)
We've all struggled with that part of the language. Its got quirks
like no other but you'll get over it.
With regard to finder paths & aliases & slash delimited vs. colon
delimited paths...
One thing that will help you with AS (took me FOREVER to get this & I
still have trouble sometimes) is that there are huge differences
between paths, folders & files. They are completely different types
of objects. Also, there are different types of paths. You have your
standard default "Mac Path" (: delimited) and you have your POSIX
(Unix '/' delimited) path. Reading up on the difference between the
two would be good info for you.
FURTHERMORE, when you are talking to the finder about something like
'desktop folder', you are not really referencing a generic system
wide data type or object (file, folder, path), you are referencing a
Finder data type or object. Sometimes they are the same between
application, but often they are not. Finder may use 'desktop folder'
in a certain way but system events uses them in a different way (more
on that below), so you need to understand from the get-go that every
application you talk to is going to look at data types in its own
way, even within the same script.
OK so lets take a look at why some of the quirks you're seeing happen.
1. Your initial script.
Try this:
tell application "Finder"
set m to class of desktop folder --constant
end tell
Then try this:
tell application "Finder"
set m to class of desktop --desktop-object
end tell
As you can see "desktop folder" and "desktop" are not AT ALL the same
object. Try these:
tell application "Finder"
set m to desktop folder as text --"desktop folder"
end tell
tell application "Finder"
set m to desktop as text --"Macintosh HD:Users:cpruitt:Desktop:"
end tell
So you see, the value of 'desktop folder' is not an object at all,
but is a predefined constant for the text string "desktop folder".
So when you realize that, of course your script didn't work. You
can't make a folder inside of a string of text! When you try to use
just "desktop" instead you reference an object (a unique Finder
object for the current users desktop item) that the Finder
understands, and therefore you can create your folder.
2. Discrepancies with Finder & System Events
Remember that I said every application interprets your script based
on its own dictionary. Try these two scripts:
tell application "Finder"
set m to (class of desktop folder) as text -- "constant"
end tell
tell application "System Events"
set m to (class of desktop folder) as text -- "folder"
end tell
The same item 'desktop folder' is considered to be two completely
different classes of object by the two applications!
This may seem like AppleScript is being inconsistent, but it's not.
Applescript leaves it up to every applications dictionary to define
its own constants and objects. You may make the case that Apple was
inconsistent when they designed the two dictionaries, but part of the
strength of Applescript is in the application specific definitions.
What if I wrote a graphics app that used folders to manage layers,
but couldn't use my own object for "folder" because it was already
taken by the finder? Every application needs to have its own unique
vocabulary.
System Events is not the same application as the Finder and therefore
has a completely different dictionary and considers 'desktop folder'
to be a completely different class than the Finder.
One mistake I think a lot of people make is just assuming that
"Finder" and "File System" are the same thing. They are not at all.
"Finder" is just like referencing "Text Edit" or "Graphic
Converter". It is its own unique application. Its purpose HAPPENS
to be related to displaying & modifying the file system, but it is
not the file system itself. When you tell the finder to create a
folder you are creating a FINDER folder, not a file system
directory. The Finder itself needs to create a file system directory
to match its new folder cause you cant have a finder folder without a
file system directory (as far as I know) but that is almost a side
effect. When you script the finder you script using its terminology
& dictionary. Never just assume that objects are the same between
applications.
You want to really blow a gasket? Try scripting the application
Adobe GoLive. they have a very extensive AppleScript dictionary for
the application. Guess how many commands work... go on, guess...
NONE! They have a fully laid out and COMPLETELY useless dictionary.
The app is effectively un-scriptable including the "required suite".
I can't even get it to respond to "activate"! So just wait until you
get to an app with a dictionary that just flat out lies to you. :-)
Matt Neuburg's book (Applescript the Definitive Guide) consistently
helps me clarify some of these questions. Its a great book, although
(sorry Matt) I haven't ever had the need to read through it from
cover to cover. It works extremely well as a topical reference when
you want a direct answer to a question such as the ones you're having
now.
Breathe deep... pace yourself... and give thanks you're not using
windows. ;-)
- Cliff Pruitt
On 9/6/05 7:35 AM, "patrick machielse" <email@hidden> wrote:
I can't get this to work
tell application "System Events"
make new folder at desktop folder
end tell
The Finder is generally responsible for all file/folder handling.
System Events handles just that - _system_ events. Creating a
directory is
not a system function, hence the need to use the Finder (or some
other app
that manages the file system.
Well, some would say that creating folders is a system function par
excellence...
More importantly, the System Events library explicitly mentions the
'make' command, with semantics almost identical to those of the
Finders 'make' command, which to me is like a promise that it should
work. Also, I _can_ move folders using "System Events".
Why is it that Finder doesn't understand "~/Desktop"? Finder should
'get with the program', the forward slash has been with us for more
than 4 years now. If anything, the use of ':' should be deprecated,
that should have been in the coffin with OS 9 :-)
Just last night I watched the 2003 WWDC AS presentation from Sal
Soghoian, in which he talked about the 5 ways to reference files in
AS, and today, in 2005, I'm still running into walls.
Trying to find sense in AppleScript,
patrick
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden