The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   OS X Developer (http://hintsforums.macworld.com/forumdisplay.php?f=27)
-   -   Finding Paths in AppleScripts (http://hintsforums.macworld.com/showthread.php?t=37084)

NovaScotian 03-27-2005 01:43 PM

Finding Paths in AppleScripts
 
Are there standard AppleScript commands for specifying the path to a folder (like the_Root_HD_or_Partition/Users/the_User_Logged_In/Library/Calendars) without knowing the name of the hard disk or partition or the name of the username of the current user or using a "choose ..." statement to find out?

To put this another way; how do I find the name of the root disk or partition and the username of user who is logged in? Given those, I can construct a path.

Finally, it is at least conceivable that the user's directory is not on the root volume, partition, or even the same machine. What then?

mark hunte 03-27-2005 02:24 PM

Part of the answer.
use something like
Code:

tell application "System Events"
        set getUser to get name of current user

end tell

or

Code:

tell application "System Events"

        set getdisk to get name of startup disk

end tell

these will get the names at least for you.

below is also a script which I had to use something like that for it to work for others

Code:

on open dropped_file
       
        tell application "System Events"
                set getUser to get name of current user
               
        end tell
        tell application "Finder"
               
                set this_item to POSIX path of dropped_file
                get this_item
                set the_store to do shell script "echo " & this_item & ">/Applications/nDrop.app/Contents/Resources/SCP.txt"
                set the_shell to do shell script "cat /Applications/nDrop.app/Contents/Resources/SCP.txt" & "|sed 's/ /\\\\ /g'" & "|sed 's/\\/Users\\/" & getUser & "/\\~/'"
               
                tell application "Terminal"
                        activate
                        do script "scp  " & "\"" & this_item & "\"" & " yourOtherhere" & ":" & "\"" & the_shell & "\""
                       
                end tell
        end tell
       
        do shell script "echo >/Applications/nDrop.app/Contents/Resources/SCP.txt"
       
end open


mark hunte 03-27-2005 02:41 PM

and just figured this


Code:

set thepath to path to home folder

NovaScotian 03-27-2005 03:05 PM

Quote:

Originally Posted by mark hunte
and just figured this
Code:

set thepath to path to home folder

Ahh, I knew there'd be a simple way. Once at the home folder, the rest is easy.

robJ 03-28-2005 06:57 AM

You might want to check out the dictionary of the Standard Additions osax. It includes the ability to get paths to several common folders. Look for "path to" in the file commands.


Code:

set lib_ to (path to library folder from user domain ¬
        without folder creation) as Unicode text

set cals_ to lib_ & "Calendars:"

-- Rob

mark hunte 03-28-2005 07:11 AM

hey Rob, do you know how to get it to work with a applescript bundled app.
(droplet)

Its supposed to work with apps, which it does if its something like iTunes, but I can not get it to work with the above??

cheers

NovaScotian 03-28-2005 09:19 AM

Quote:

Originally Posted by robJ
You might want to check out the dictionary of the Standard Additions osax. It includes the ability to get paths to several common folders. Look for "path to" in the file commands.


Code:

set lib_ to (path to library folder from user domain ¬
        without folder creation) as Unicode text

set cals_ to lib_ & "Calendars:"

-- Rob

The hard part of these constructions for me are the bits like "from user domain without folder creation". That's where I always fall down. "path to library folder" will get you the root library folder and I didn't know the "from user domain" terminology. Thanks.

mark hunte 03-28-2005 09:24 AM

I would be nice if the

with folder creation
worked on Normal folders, save all that if exist crap.

robJ 03-28-2005 02:19 PM

Quote:

Originally Posted by mark hunte
hey Rob, do you know how to get it to work with a applescript bundled app.
(droplet)

Its supposed to work with apps, which it does if its something like iTunes, but I can not get it to work with the above??

cheers

I'm not sure what you are asking here. Can you elaborate?

-- Rob

mark hunte 03-28-2005 04:05 PM

In the dictionary of the Standard Additions
it has
path to: Returns full path name to the folder or application specified
path to application support/applications folder/desktop/desktop pictures folder/documents folder/favorites folder/Folder Action scripts/fonts/frontmost application/help/home folder/internet plugins/keychain folder/library folder/modem scripts/movies folder/.
.. -- the folder or application to return


As you can see it states "path to: Returns full path name to the folder or application specified"

This works with iTunes but not with a script.app.

Since the scripts are classed as a .app I wondered if there is a way to point to them using this method

robot_guy 03-28-2005 08:24 PM

Quote:

Originally Posted by mark hunte
This works with iTunes but not with a script.app.

If you're trying to point to a file contained within an AppleScript application bundle, you can use "path to me" to get a reference to the app itself, then add the rest of the required path as a string.

For example:
Code:

set inputWord to (text returned of (display dialog "Word to translate:" ¬
      default answer "") as string)
                set objectFolder to (path to me) as string
                set outputWord to do shell script ((POSIX path of (objectFolder)) ¬
      & "Contents/Resources/ObjectCode/pigLatin.out " & inputWord)

Unfortunately, it seems you can't test this by running the script from within Script Editor, because the "path to me" command returns a reference to Script Editor.app (since in that case, it's the current app).

I don't know of a workaround, other than resaving the script.app after every change you make to the script, and launching the app to see if you've succeeded.

robJ 03-29-2005 06:44 AM

Quote:

Originally Posted by mark hunte
As you can see it states "path to: Returns full path name to the folder or application specified"

You can ask for the name of a running app with something like this: path to application "name of app"

If you need the path of an app that isn't running, you might need to get more creative with something like this:

Code:

-- The app's signature can be found in the info.plist within the package.
-- It's the "CFBundleIdentifier" value.

tell application "Finder" to ¬
        set appPath to application file id ¬
                "com.apple.AddressBook" as Unicode text

You might need to add this info to your script.app's package. I haven't created a package so I can't say for sure.
-- Rob

mark hunte 04-14-2005 04:15 PM

Thanks Guys, just came back to this thread and saw the replies.

mark hunte 04-18-2005 05:12 PM

Just found time to look at this again. and the script now includes the path change. so you can put the app any where, and it will find its contents folder so it can write to the SCP.txt file inside it.
It also will now ask for the server name you want to send to.


Code:

on open dropped_file
        tell application "Finder"
                activate
                display dialog " enter Receveing Server Name" default answer "myg5"
        end tell
        tell application "System Events"
               
               
                set the_R_servername to the text returned of the result
                set getUser to get name of current user
                set objectFolder to (path to me) -- this sets the path to the app the script runs from ( i.e  this app )
                set objectFolder to POSIX path of objectFolder -- changes the path from ( path:to:app ) to (path/to/app)
        end tell
        tell application "Finder"
               
                set this_item to POSIX path of dropped_file
                get this_item
                set the_store to do shell script "echo  " & this_item & "> " & objectFolder & "/Contents/Resources/SCP.txt"
               
                set the_shell to do shell script "cat " & objectFolder & "/Contents/Resources/SCP.txt" & "|sed 's/ /\\\\ /g'" & "|sed 's/\\/Users\\/" & getUser & "/\\~/'"
               
               
                tell application "Terminal"
                        activate
                        do script "scp  " & "\"" & this_item & "\" " & the_R_servername & ":" & "\"" & the_shell & "\""
                       
                end tell
        end tell
       
        do shell script "echo >" & objectFolder & "/Contents/Resources/SCP.txt"
       
       
end open

I guess the next trick is to get it to create folders if they do not exist

lc7777 06-16-2005 03:28 PM

Hi all,

On a related topic, I have a renaming script that I have to add the current path at the beginning of the script before executing. Is there a nice little snippet of codes that can ease the pain by providing the script the 'current path' value?

Thanks in advance.

property sourceFolder : "hd2:gh0075:print:"
set filenameList to read file "hd2:gh0075:print:filenames.txt" using delimiter {return}
set {oldDelims, text item delimiters} to {text item delimiters, tab}

tell application "Finder"
repeat with aFile in filenameList
set oldname to (sourceFolder & (text item 1 of aFile)) as string
set newName to text item 2 of aFile
set name of file oldname to newName
end repeat
end tell

robJ 06-16-2005 05:18 PM

Quote:

Originally Posted by lc7777
On a related topic, I have a renaming script that I have to add the current path at the beginning of the script before executing. Is there a nice little snippet of codes that can ease the pain by providing the script the 'current path' value?

What do you mean by current path? The path to your script? If so, you can get its path, if it is saved as an application, with the following:

Code:

set my_path to path to me as Unicode text
Note: When you run this in Script Editor, it will return the path to Script Editor.app. When the script.app is launched, it will return the path to the script.

-- Rob

mark hunte 06-16-2005 06:31 PM

Or if you mean the folder path to you files.

You must have the window of the folder open, for this script.
Code:

try
        tell application "Finder"
                set sourceFolder to (folder of the front Finder window) as alias
                count items in folder sourceFolder ---- example on action with the path
        end tell
on error
        set error_message1 to "Open a Finder window before running the script."
        display dialog error_message1 buttons {"OK"} default button 1
end try



or the script below by selecting a folder first

Code:

try
        tell application "Finder"
                set sourceFolder to selection as alias
                count items in folder sourceFolder -- example on action with the path
        end tell
on error
        set error_message1 to "Select a Folder,  before running the script."
        display dialog error_message1 buttons {"OK"} default button 1
end try


lc7777 06-16-2005 06:53 PM

I would like the script to know where it resides.

A typical process would be for me to drag this script into a folder containing the files to be replaced, along with the filenames.txt which I create and place in the same folder. This tab-delimited file provides the original and new filenames for the script to work with. For example:

This_folder
abc.pdf
def.pdf
ghi.pdf
filenames.txt
batch_rename.scpt

I would like the script to learn the full path of "This_folder" without having to hardcode it into the script manually.

Now I'll study all of your replies.

Thanks again, all.

lc7777 06-16-2005 08:38 PM

Yes, by 'current path', I do mean folder where the script and files to be renamed reside.

But now, I'm having a little trouble integrating your suggestion into my existing script. I took away the Property section (see my original post) and try to integrate your codes into the Tell section, unsuccessfully. Do I need to restore the Property section, and try to integrate your codes into that section somehow?

I'm getting the error "Finder got an error: Can't get text item delimiters." on line 5 below. What does the line "set {oldDelims, text item delimiters} to {text item delimiters, tab}' do exactly?

Code:

tell application "Finder"
        set sourceFolder to (folder of the front Finder window) as string
        set filenames to sourceFolder & "filenames.txt" as string
        set filenameList to read file filenames using delimiter {return}
        set {oldDelims, text item delimiters} to {text item delimiters, tab}
        repeat with aFile in filenameList
                set oldname to (sourceFolder & (text item 1 of aFile)) as string
                set newName to text item 2 of aFile
                set name of file oldname to newName
        end repeat
end tell

Thanks again.

robot_guy 06-17-2005 08:03 AM

Try replacing the line set {oldDelims, text item delimiters} to {text item delimiters, tab} with set {oldDelims, AppleScript's text item delimiters} to {AppleScript's text item delimiters, tab}...

(Because this line occurs in a Finder tell block, you need to specify that it's AppleScript's text item delimiters you're accessing/setting; otherwise, Finder is searching its own dictionary for text item delimiters, and coming up empty.)

lc7777 06-17-2005 12:06 PM

That worked! Amazing. Thanks a million. And also thanks to Mark Hunte for the initial help. You guys are great!

Quote:

Originally Posted by robot_guy
Try replacing the line set {oldDelims, text item delimiters} to {text item delimiters, tab} with set {oldDelims, AppleScript's text item delimiters} to {AppleScript's text item delimiters, tab}...

(Because this line occurs in a Finder tell block, you need to specify that it's AppleScript's text item delimiters you're accessing/setting; otherwise, Finder is searching its own dictionary for text item delimiters, and coming up empty.)


Carl Stawicki 06-17-2005 01:07 PM

There's a nice list of standard "path to" commands at macscripter.net :

http://macscripter.net/faq/get_the_f...id=17_0_10_0_C


All times are GMT -5. The time now is 06:03 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.