How to retrieve server path of a selected item in finder - applescript

Is there a way to retrieve the server path name of the selected item within finder with applescript?
I've tried the properties URL and POSIX path but they just point to the local file system instead of the server path for example: smb://server/folder1/folder2/filename
code so far:
tell application "Finder"
set selectedItem to the selection as alias
URL of selectedItem --ouput: file://localhost/Volumes/server/folder1/folder2/filename
POSIX path of selectedItem --output: /Volumes/server/folder1/folder2/filename
end tell
thanks for the help

I think this has to do with the way directories work in OS X (and every *nix). AFAIK there are no UNC style paths on a mac. So the only workaround is to make a database of the available windows shares, look up the name of the volume of the file and spit out a 'file://some/server/path' style URL based on that.
At least that's what I did when I had the same problem. Note that smb:// urls won't work for most web apps.
this and this shed some light on to it.

Related

How do I deploy my applescript to multiple devices?

This is going to sound like a dumb question because I can't figure out how to word it properly. But basically I want to create a package that contains a photo and then a script to set that photo as the wallpaper for that device. This is specifically for Catalina so I am not able to modify the Desktop Photos folder. The issue that I am running into is that if I download the photo the file path is going to be attached to the user. But I can't use something like $currentuser as part of of the file path. Just hoping for any ideas at all on how to get around this. I am very new to applescript as well so probably something that I am missing. Thank you.
tell current desktop
set picture to file "/Users/lemur/Pictures/bluemoon.jpg" as POSIX file
end tell
end tell
Here are five two-line examples which could be helpful. Run each and inspect the result:
set fd to path to desktop
return fd
set fd to path to applications folder
return fd
set fd to (path to applications folder from user domain)
return fd
set fd to path to pictures folder
return fd
set fd to path to desktop pictures folder
return fd
As you are talking to System Events anyway the syntax is pretty simple
tell application "System Events"
tell current desktop
set picture to file "bluemoon.jpg" of pictures folder
end tell
end tell
pictures folder is always the pictures folder of the current user

Apple Automator take screen capture and save to user-specified location

So, I'm trying to use Automator under Mac OS Yosemite to create a service to allow a user to take a screenshot and save it to a location they specify, through some sort of "Save As" dialog. It seemed like it should be easy, but for some reason I'm running into difficulty with it. The screenshot component is easy, using the "Take Screenshot" action in Automator, but it's the saving it to a custom location that's causing me problems.
After trying a few different approaches, it seemed the easiest thing to do was to save the screenshot to a fixed directory/filename from within the "Take Screenshot" action, and then (using AppleScript) rename it in that directory, and move it to the user-specified target directory. So, I added a "Run AppleScript" action to my service. In it, I generate the dialog to choose a file name/path, using the choose file name command in AppleScript. I'm trying to split up the file name from the path, so that I can rename the file I save in "Take Screenshot," and then move it to the path that I'd like to save it at. I can get the full path, but am having problems just getting the filename from the path—and I've tried a variety of suggestions from what I've seen online. In my screenshot, the error shown was from attempting to do
I'm not set by any means on this flow, so if anyone has any better suggestions on how to do what I'm trying to do, by all means please let me know. Otherwise, if someone's able to just tell me how I can extract the filename from the path (and also if there's some special way you have to use that string to rename the file) that'd be great!
AppleScript code pictured in screenshot:
on run {parameters}
set thePath to (choose file name with prompt "Where would you like to save your file?")
tell application "Finder"
display dialog thePath as string
end tell
set UnixPath to POSIX path of (thePath as text)
display dialog UnixPath
end run
I tried this but it didn't work:
set basePath to POSIX path of (parent of (thePath) as string)
Thanks for checking it out!
An easy way would be to use the command line tool "screencapture". It has many options you can choose. See its man page. Here's an example that you can run as an applescript directly or you could put this inside an applescript automator action if you want.
Good luck.
set thePath to (choose file name with prompt "Where would you like to save your file?")
do shell script "screencapture -mx -T1 " & quoted form of (POSIX path of thePath & ".png")
I've created a Folder action in Automator. Choose Desktop. Add Find Finder objects (search: Desktop). Add Move Finder Object and choose your preferred destination. This will automatically move all your screenshots.

Can't get relative location of image to use, having to use absolute path

Im building my first app in Xcode using Applescript Objc - I have managed to get it to swap an image (in an outlet called imageViewOne) based on the code below:
-- Declarations
property ImagePath : missing value
property imageViewOne : missing value
property NSImage : class "NSImage"
property parent : class "NSObject"
-- set path as variable
set my ImagePath to "/Users/monkey/XCode/testapp/green.png"
set theImage to initWithContentsOfFile_(POSIX path of ImagePath) of alloc() of class "NSImage" of current application
setImage_(theImage) of imageViewOne
While this works, the problem is that I need to find a way to use a relative path, not "/Users/monkey/XCode/testapp/green.png" (the png is included in the XCode project).
Any help would be well received - as you can see I am new to this!
Would something like this work for you?
set parentDirectory to POSIX path of ((path to me as text) & "::")
AppleScript was originally created for the Classic Mac OS, which did not have a concept of a current directory, as UNIX does. It had "working directories", which are often confused for "current directory" but are quite different.
What you'll need to do (I expect) is find the absolute path of something you can depend on, such as the location of your script, or of an application bundle if your script is inside a bundle, then append your relative path to that absolute path.
In the case of my own iOS App, in Objective-C with Cocoa Touch, I can get my app's Documents folder with:
- (NSString*) documentDirectoryPath
{
return [NSHomeDirectory() stringByAppendingPathComponent: #"Documents"];
}
Can AppleScript call NSHomeDirectory, or does it provide a functional equivalent?
Note that NSHomeDirectory provides the current app's directory, not the user's home.
edit: I don't actually know whether AppleScript provides UNIX-style current directories. Possibly it does. However, a GUI app cannot count on having any specific current directory at the time it launches. Usually it will be the Finder's current directory, for OS X, or for iOS it would be the current directory of the program that presents the app icons and launches them - the program that displays the home screen. I don't know what it's called.
Only Classic Mac OS, as well as Carbon on Mac OS X have the concept of working directories. I don't think Windows has them. UNIX processes can have only one current directory, but Classic Mac OS programs can have as many working directories as memory allows.
Working directories were originally a hack to enable very old - Mac 128k - executables to do file I/O on Heirarchical Filesystem floppy disks. The 1984 Superbowl Mac had "Macintosh Filesystem" floppies that were flat and not heirarchical. That is, one could not have folders within folders.
To be completely clear, I'm discussing working directories because the ORIGINAL AppleScript used them. I really do not know whether AppleScript has been revised to add the concept of the - one per-process - UNIX-style current directory.
Even if AppleScript does provide the current directory, you'll need to set it explicitly, because as I said you cannot count on the current directory at launch being what you expect.
I worked it out.
set ImagePath to current application's NSBundle's mainBundle()'s bundlePath() as text & "/Contents/Resources/imageName.png"
Thanks.

How to reference a file on users desktop with Applescript

I'm pretty new to Applescript and am trying to reference a file on the currently logged-on user's desktop, but I don't know how. In POSIX you'd use the tilde character to specify the user's home folder, but that doesn't seem to work in applescript. I've read other posts about how "desktop" is already defined as a keyword applescript understands, but I don't know how to specify a sub-folder of the pre-defined keyword (desktop/folder doesn't work).
Any help would be appreciated.
set theTargetFolder to ((path to Desktop Folder) & "name of target folder") as string
returns "Macintosh HD:Users:username:Desktop:name of target folder"

Preferred path to applications on OSX?

I want to be able to run a text editor from my app, as given by the user in the TEXT_EDITOR environment variable. Now, assuming there is nothing in that variable, I want to default to the TextEdit program that ships with OSX. Is it kosher to hardcode /Applications/TextEdit.app/Contents/MacOS/TextEdit into my app, or is there a better way to call the program?
Edit: For the record, I am limited to running a specific application path, in C. I'm not opening a path to a text file.
Edit 2: Seriously people, I'm not opening a file here. I'm asking about an application path for a reason.
In your second edit it makes it sound like you just want to get the path to TextEdit, this can be done easily by using NSWorkspace method absolutePathForAppBundleWithIdentifier:
NSString *path = [[NSWorkspace sharedWorkspace] absolutePathForAppBundleWithIdentifier:#"com.apple.TextEdit"];
Mac OS X has a mechanism called "uniform type identifiers" that it uses to track associations between data types and applications that can handle them. The subsystem that manages this is Launch Services. You can do one of two things:
If you have a file with a reasonably well-known path extension, e.g. .txt, you can just ask NSWorkspace to open the file in the appropriate application.
If you don't have a well-known path extension, but you know the type of data, you can ask Launch Services to look up the default application for that type, and then ask NSWorkspace to open the file in that specific application.
If you do it this way you'll get the same behavior as the Finder, and you won't have to fork()/exec() or use system() just to open a file.
I believe hardcoding "Applications" will not work if the user's language setting is not English. For example in Norsk the "Applications" folder is named "Programmer".
The Apple document on internationalization is here. Starting on page 45 is a section on handling localized path names.
I believe that Mac OS X provides a default application mechanism, so that .txt will open in TextEdit.app or Emacs or GVim or whatever the user has specified. I couldn't find anything online however.
You could run following command from your application:
open <full path to text file>
This will open the text file in the default text editor. You can open any file type using open command.

Resources