Re: Stumped with open file
Re: Stumped with open file
- Subject: Re: Stumped with open file
- From: Kai Edwards <email@hidden>
- Date: Thu, 21 Mar 2002 05:55:42 +0000
on Wed, 20 Mar 2002 12:04:41 -0500, "Leif E. Wells" <email@hidden>
wrote:
>
Date: Wed, 20 Mar 2002 12:04:41 -0500
>
To: <email@hidden>
>
From: "Leif E. Wells" <email@hidden>
>
Subject: Stumped with open file
>
>
I apologize if this is a question that has been asked before. I just
>
joined the list and after re-reading the AppleScript documentation I
>
am still having problems. (Are there archives somewhere?)
Try:
<
http://www.lists.apple.com/mailman/listinfo/applescript-users>
...click on the 'archives' option and follow the instructions from there.
Also, if you haven't already got a copy of the AppleScript Language Guide
(ASLG) - and missed the recent posting on the subject - then go to:
<
http://developer.apple.com/techpubs/macosx/Carbon/interapplicationcomm/Appl
eScript/AppleScriptLangGuide>
(snip - background info)
>
The main problem is with relative paths or proper syntax, I guess.
>
Since I do not know where the user will be downloading the Flash
>
projector, AppleScript, and PDF file, I do not know what the absolute
>
path to the PDF file will be.
>
>
I have read that all I need to do is:
>
>
tell application "Finder"
>
open file "CS_User_Tutorial.pdf"
>
end tell
>
>
My assumption (I know, never assume) is that if the file and the
>
script/runtime script are in the same folder, the file should open.
>
It does not. I am testing from the script editor and running a
>
runtime app. I get "Cannot find file" errors, and yes the file is
>
there.
>
>
What am I missing?
Well, you're on the right track with the setup, Leif. The most effective way
to achieve what you're trying to do is to have the relevant file and the
script in the same folder. And yes, to make the following script work, it
needs to be saved as an application.
You're just a little short on information about the path to the file you
want to open. To specify that, you need to do something like this:
-----------------------------------------------------------------
tell application "Finder" to open my neighbour("CS_User_Tutorial.pdf")
on neighbour(fileName)
set myPath to path to me as string
set {TID, text item delimiters} to {text item delimiters, ":"}
set ourFolder to myPath's text 1 thru text item -2
set text item delimiters to TID
ourFolder & ":" & fileName as alias
end neighbour
-----------------------------------------------------------------
Now although that may look a little 'hefty', it is fairly efficient - and
the 'neighbour()' handler could be used to specify the path to any
neighbouring file. (You need only change the filename.)
But what does the script actually do?
--------------------- Line 1 ---------------------
tell application "Finder" to open my neighbour("CS_User_Tutorial.pdf")
--------------------------------------------------
This is a just a tell block on one line. We could equally have worded it
like this:
tell application "Finder"
open my neighbour("CS_User_Tutorial.pdf")
end tell
But however we word it, it clearly tells the Finder to open something. The
word 'my' actually clarifies that what follows can be found within the
script itself. The 'neighbour()' bit is what calls the 'neighbour' handler.
And anything within the caller's parentheses (in this case -
"CS_User_Tutorial.pdf") is information to be passed to the handler.
So before the Finder can open anything, the result of the 'neighbour'
handler must first be evaluated. Now let's take a look at that handler...
--------------------- Line 2 ---------------------
on neighbour(fileName)
--------------------------------------------------
This line does a couple of things. First, it marks the point in the script
at which the 'neighbour' handler begins. Second, it takes whatever was
passed (in parentheses) by the caller - and stores it in a variable called
'fileName'. So the value of 'fileName' is now "CS_User_Tutorial.pdf".
--------------------- Line 3 ---------------------
set myPath to path to me as string
--------------------------------------------------
The words 'set myPath to...' tell us we're going to store something in the
variable called 'myPath'. The result of 'path to me' is an alias - such as:
--> alias "Macintosh HD:Projects:My Script"
(If you try running the script in script Editor, however, you're more likely
to get something like: alias "Macintosh HD:Apple Extras:AppleScript:Script
Editor". This is because the application running the script is Script
Editor. You need to save the script as an application - so that it then
becomes the target of the "path to me" command.)
Anyway, the next part of the script ('as string') says that we want to
coerce the previous result (alias "Macintosh HD:Projects:My Script") to
text. This coercion simply results in the string:
--> "Macintosh HD:Projects:My Script"
- so now we have some text to work with!
--------------------- Line 4 ---------------------
set {TID, text item delimiters} to {text item delimiters, ":"}
--------------------------------------------------
This line uses a couple of lists to issue 2 commands - so it's really 2
lines in one. We could equally have said this instead:
set TID to text item delimiters
set text item delimiters to ":"
Text item delimiters are an AppleScript property that defines the separator
used for lists coerced to text - or for getting text items. The default
value for delimiters is "". However, the current value may sometimes be
something else, so - before changing it, we should save it for reinstatement
later. Here, it's stored in a variable called 'TID'.
That frees us up to change the text item delimiters. And as you can see,
we've set them to ":".
(...you'll learn why in a moment - if you haven't already guessed)!
--------------------- Line 5 ---------------------
set ourFolder to myPath's text 1 thru text item -2
--------------------------------------------------
Now we're going to store something in the variable 'ourFolder'.
The phrase 'myPath's text 1 thru text item -2' is similar to saying
something like "all text from the first character to the second-to-last text
item of myPath". (Oh yeah - negative indexes. They go like this: item -1 =
last item, item -2 = second-to-last item, etc.)
Remember that the value of 'myPath' is:
--> "Macintosh HD:Projects:My Script"
So the first text character of myPath is "M".
However, since we changed AppleScript's text item delimiters to ":", we can
now also identify the text items between each ":" - so that:
text item 1 = "Macintosh HD"
text item 2 = "Projects"
text item 3 = "My Script"
The second-to-last text item of myPath is "Projects" - so 'myPath's text 1
thru text item -2' finally evaluates as:
--> "Macintosh HD:Projects"
...which is now stored in the variable 'ourFolder'.
--------------------- Line 6 ---------------------
set text item delimiters to TID
--------------------------------------------------
Now that we've finished with the text item delimiters, we've restored them
to their previous value.
--------------------- Line 7 ---------------------
ourFolder & ":" & fileName as alias
--------------------------------------------------
Well, that should be simple enough: 'ourFolder' ("Macintosh HD:Projects") &
":" & 'fileName' ("CS_User_Tutorial.pdf") evaluates to:
--> "Macintosh HD:Projects:CS_User_Tutorial.pdf"
- which is still just a string, so the Finder won't yet fully understand it.
However, coerce that to an alias (with 'as alias') and we've got:
--> alias "Macintosh HD:Projects:CS_User_Tutorial.pdf"
And - because it's the last evaluation of the handler, this is what's
automatically returned to the original caller.
--------------------- Line 8 ---------------------
end neighbour
--------------------------------------------------
No prizes for guessing what that did!
--------------------- Line 1 ---------------------
tell application "Finder" to open my neighbour("CS_User_Tutorial.pdf")
--------------------------------------------------
Yes, here we are again at the first line. But this time we know what 'my
neighbour("CS_User_Tutorial.pdf")' has been evaluated as. In fact, the
command now means this:
tell application "Finder"
open alias "Macintosh HD:Projects:CS_User_Tutorial.pdf"
end tell
Which, as long as the file is where it should be, the Finder should open as:
--> file "CS_User_Tutorial.pdf" of folder "Projects" of startup disk
--------------------------------------------------
Sorry for the rather lengthy answer, but the subject does crop up from time
to time - and can cause a little confusion.
If you've managed to get this far, congratulations for staying awake. I hope
my attempt at an explanation hasn't confused you even more!
Good luck with your scripting.
Kai
--
**********************************
Kai Edwards Creative Resources
1 Compton Avenue Brighton UK
Telephone +44 (0)1273 326810
**********************************
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.