This is a bit of an odd question. Sorry if I sound stupid here, but this is the first time i’ve really started messing with folder paths, and variables.
The idea here is that I have this file structure
/My Script/Myscript.app
/My Script/Myscriptsfolder/
Obviously, Myscript.app is the script I will be running when all is said and done. It will get the contents of the folder Myscripts, put it in a list, and then display that list. The user picks an item, and then the script opens that item.
The reason I’m using a folder is because the idea is that it can be expanded with the users own scripts, and so that things are nice and organized for the end user, and so that they can have all their scripts in one spot, instead of cluttering the main folder.
Now then, here is where the problem lies. (I’ve changed names to make it easier to read)
How to make this self-contained? I’m hoping to be able to distribute it when I’m done, so I can’t use set paths
How do I get the path to Myscriptsfolder? So far, I’ve got
tell application "Finder"
set mypath to (container of (path to me) as alias)
set Myscripts to name of every item of folder mypath
end tell
The (container of (path to me) as alias) was advice given to me from another forum. I’m not that good with this paranthesies stuff yet
Obviously, all this does is get the contents of the folder that the script is in, in this case, “My Script”, but doesn’t do me any good in getting the contents of the folder which holds the scripts, which is another level down.
Thats the only questions i’ve got for now. Hopefully y’all can help me out I’ve tried everything I can think of, searched around with no luck, but noticed that these boards seem to come up quite a bit.
Since you know the name of the embedded folder, doesn’t this work?
tell application "Finder"
set mypath to (container of (path to me) as alias) -- the path to the folder holding the script
set Myscripts to ((mypath as string) & "Myscripts:") as alias -- the path to a folder in mypath, name known.
end tell
set my_ref to (path to me)
tell application "Finder"
set my_f to (container of my_ref) as string
end tell
set scripts_f to (my_f & "My Scripts") as alias
tell application "Finder"
set my_scripts to (name of every file of scripts_f)
end tell
if my_scripts is {} then return
set user_script to (choose from list my_scripts)
set script_ref to ("" & scripts_f & user_script) as alias
run script script_ref
Wow! That really has helped. I messed it around a bit for my script, but I’m making huge headway on my script. I know i’ve gone to the right place.
Although, I would like to use this as a bit of a learning opportunity, for me anyways. I put a number next to each line I have a question about, and then put the corrisponding question below… Seeing as I’m relatively new to this more advanced stuff, i’d like to learn a bit more about it.
set my_ref to (path to me) --1--
tell application "Finder"
set my_f to (container of my_ref) as string --2--
end tell
set scripts_f to (my_f & "My Scripts") as alias --3--
tell application "Finder"
set my_scripts to (name of every file of scripts_f) --1--
end tell
if my_scripts is {} then return --4--
set user_script to (choose from list my_scripts) --5--
set script_ref to ("" & scripts_f & user_script) as alias --6--
run script script_ref
I can see what this does, but what I don’t know is why the parenthesies are there.
Same paranthesies question, as well as a few others. First… Container of my_ref? What is that? and second… what is it mean to be set “as string”? Also, why does this seem to mention the same thing twice. (as in 1) Isn’t this just 2 ways of getting the path to my script?
Whoa whoa whoa. The way I read this, this sets the variable “scripts_f” to my_f, which I guess is the path to my folder, and “myscripts”. Is that just saying that the folder “myscripts” is in the last folder of “my_f”?
This means if there is nothing in the folder, then return. But return what?
From what I gather, this means that it sets the variable “user_script” to whatever the user picks from a list of “my_scripts” which is the folder holding all my scripts. Why not use that list dialog command?
I guess I get confused a few times here. first, whats up with all that stuff inside the paranthesies? Particularly the pair of empty quotes? and second, whats up with setting stuff “as alias”?
I hope you can at least answer a few of my questions This is my first venture into the world of variables, paranthasies, and paths, and its a big world indeed.
I’d most like to know about the importance of paranthasies, and the reasons"as string" and “as alias” gets used, though i’d be grateful for any of my questions that get answered. Hopefully y’all can help a guy whos trying to learn this stuff
I believe that, in this specific case, the parenthesies are optional. (Personally, I tend to use lots of parentheses.) However consider this:
-- path to me must be in parenthesies; Otherwise, "as Unicode text" would cause an error.
set resourceFolder to (path to me as Unicode text) & "Content:Resources:"
Container is part of the Finder’s dictionary. It is a property of a Finder item which returns “the container of the item”; That is, the folder which contains the item (the parent folder, if you will), not the item itself (such as path to me).
Why get it as a string? We’ll come back to that in the next question.
As for the parentheses. Though not technically required in this case, but they can help readability. Both of these examples would return the same result.
-- The Finder gets the container item, which is a document object, which is then coerced to a string.
tell application "Finder" to return container of whateverItem as string
tell application "Finder" to return (container of whateverItem) as string
However, consider the example below. The parentheses cause whateverItem to be coerced to a string first, and then the Finder tries to get the container of that string. The Finder can’t do this, so it throws an error.
tell application "Finder" to return container of (whateverItem as string)
–
Yes, that is what it is saying.
Remember that ˜as string’ from question 2? We did that so that we could add another string (“My Scripts”) on it, and then converted it into an alias we can use later on.
If you don’t give return a value [to return], then it returns nothing.
First sentence, yes. I’m not sure what you mean by “that list dialog command”.
(BTW kel, your alias coercion on the next line will throw an error if the user cancels from the list.)
The parentheses are the same as before, they make sure things happen in the right order. (Some strings are concatenated and then coerced to an alias.)
The empty quotes are a “simpler” (lazier?) way of making sure the other items in the parentheses are coerced to a string. Consider thse examples:
-- You can't concatenate numbers, so a list is returned
(1 & "-test-" & 2)
-- The other items are concatenated with the first string (numbers are automatically coerced to strings)
("" & 1 & "-test-" & 2)
I believe that the empty string can also help deal with text encoding issues, but that’s a weak spot for me, so I’ll leave it for someone else to discuss.
What, exactly, is a string, anyways? Sorry, Applescript is the first programming language i’ve learned
Same goes for alias. Does it take whatever you told it (I.E- path to a folder) and just make it so that next time you use it, it just inputs the entire path?
I’m also confused as to usage of those terms. I think explaining what they are/do would help clear that up, though.
2. The list thing I ment is instead of a dialog box, its a list. Its the same effect produced by the script above, but with a different command… I think its something like
tell application "finder"
choose from list list_to_display as list
end tell
A string is another word for text. (Traditionally called string because it’s a(n) (ordered) string of characters.)
An alias is an object that represents an actual file/folder.
Er, that’s what kel used:
set user_script to (choose from list my_scripts) --5--
Edit: Here’s a different version of kel’s script for you to look at (same effect):
tell application "Finder"
set scriptsFolder to folder "My Scripts" of (container of (path to me)) as alias
set scriptList to (name of files of scriptsFolder)
end tell
choose from list scriptList
get ("" & scriptsFolder & result) as alias
run script result
For the sake of simplicity, I’ll refrain from adding error handling (for the list in particular).
I’d advise anyone with a serious interest in AppleScript to check out the AppleScript Language Guide. (While it hasn’t been updated in quite some time, much of the content is still highly relevant.)
For what it’s worth, we should be able to get Finder to do most of the legwork, here - without any text manipulation at all. We could also include a filter to omit the currently running script from the list - as well as a simple trap for any errors (usually resulting from attempts to get a property [name] of zero items, or to extract an item from a boolean value [false] - which doesn’t have any items).
To open sibling files:
tell application "Finder"
set {name:myName} to path to me
tell (get container of result) to try
open file ((choose from list (get name of files whose name is not myName))'s item 1)
end try
end tell
To run scripts, perhaps an added coercion:
tell application "Finder"
set {name:myName} to path to me
tell (get container of result) to try
run script (file ((choose from list (get name of files whose name is not myName))'s item 1) as alias)
end try
end tell