Working with a file by pathname/URI using Finder + ScriptingBridge

I'm using the generated Finder.h headers from the sdef utility, and it appears that a lot of Finder SB methods require objects of type FinderItem to do anything meaningful with.

How would I go about creating one of them objects based on a path to or URI of the file?

The farthest I've got is the [SBObject initWithProperties] method briefly mentioned in the SB guide but have no clue where to go from there. The basic AppleScript I'd like to translate into Objective-C is then, to put it yet another way:

set myFile to POSIX file "/untitled folder/funneh/keyboardcat.mov"

--------------Solutions-------------

I'm not sure about SBObject, but if you want a FinderItem*, this is how you could get one.

NSString* pathString = [@"/untitled folder/funneh/keyboardcat.mov" stringByExpandingTildeInPath];
NSURL* theFileURL = [pathString fileURLWithPath:pathString];
FinderApplication* finder = [SBApplication applicationWithBundleIdentifier:@"com.apple.finder"];
FinderItem * theItem = [[finder items] objectAtLocation: theFileURL];

If you just want the FinderItem object then slf's code will work if you change the line:

NSURL* theFileURL = [pathString fileURLWithPath:pathString];

to

NSURL* theFileURL = [NSURL fileURLWithPath:pathString];

But if you want a HFS style path then I found this snippet.

NSString* path = [(NSString*)CFURLCopyFileSystemPath((CFURLRef)theFileURL, kCFURLHFSPathStyle) autorelease];
NSLog(@"path= %@",path);

Which returns a string of "Hard Disk:untitled folder:funneh:keyboardcat.mov"

The snippet can be found here.

After reading a very detailed AppleScript related answer, I decided to stick with appscript, which made things much easier:

FNApplication *finder = [[FNApplication alloc] initWithName:@"Finder.app"];
FNReference *ref = [[finder files] byName: @"/users/movies/kitteh.mov"];

Category:objective c Time:2010-07-12 Views:0

Related post

  • How to read a file from a URI using StreamReader? 2010-08-11

    I have a file at a URI that I would like to read using StreamReader. Obviously, this causes a problem since File.OpenText does not support URI paths. The file is a txt file with a bunch of html in it. I have multiple web pages that use this same piec

  • Indexing file paths or URIs in Lucene 2010-09-10

    Some of the documents I store in Lucene have fields that contain file paths or URIs. I'd like users to be able to retrieve these documents if their query terms contain a path or URI segment. For example, if the path is C:\home\user\research\whitepape

  • How to extract the file name from URI returned from Intent.ACTION_GET_CONTENT? 2011-04-06

    I am using 3rd party file manager to pick a file (PDF in my case) from the file system. This is how I launch the activity: Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType(getString(R.string.app_pdf_mime_type)); intent.addCategor

  • How to read a text file from a Uri, and save it, using Silverlight/ C#? 2011-05-19

    i want to read a file by its Uri, and ask the user to store the file. This has to be done using Silverlight. Ive tried the following, and it didnt work.. client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted); private

  • How can i perform a GET request from a separate php file to a uri in my cakephp platform 2011-07-03

    I'm new with cakephp and I've just finished putting my controllers, models and views in place.I am able to get information from my database through the browser, but i want to achieve this using a seperate php file where i perform a get request. How c

  • How can we have the file and pathname show on all office documents without having to do this manually for each computer 2012-08-22

    Our office uses Word 2007. We would like to have the file and pathname appear in all word and excel documents. I think I might need a macro to install on all computers. Is this possible? I know how to add the file and path name but I would like this

  • file and pathname 2012-05-06

    I used to be able to add a button to my taskbar that would automatically insert the file and pathname of the document, onto the document. I would then be able to refer to that and locate the document. What is that taskbar button called now? ---------

  • How can I get file extension from URI for an rails application 2012-01-06

    I want to find File extensions like .gif,.jpg,.txt from the uri like . --------------Solutions------------- You can use an extname method of File url = "http://testasp.vulnweb.com/avatars/noavatar.gif" File.extname(url) #=> .gif There may be an ea

  • Get file name from URI string in C# 2009-07-09

    I have this method for grabbing the file name from a string URI. What can I do to make it more robust? private string GetFileName(string hrefLink) { string[] parts = hrefLink.Split('/'); string fileName = ""; if (parts.Length > 0) fileName = parts

  • Using an image file vs data URI in the CSS 2009-09-24

    I'm trying to decide the best way to include an image that is required for a script I've written. I discovered this site and it made me think about trying this method to include the image as a data URI (defined by RFC 2397) since it was so small - it

  • Java: constructing a file from a URI? 2010-02-28

    I need to obtain a File object from a URI, working in Java, but keep getting a length of zero - though I know the file size is not zero. I need the File object to pass to another constructor. I'm not sure if it's because I'm constructing it in the wr

  • How to determine the file extension of a file from a uri 2010-07-13

    Assuming I am given a URI, and I want to find the file extension of the file that is returned, what do I have to do in Java. For example the file at http://www.daml.org/2001/08/baseball/baseball-ont is http://www.daml.org/2001/08/baseball/baseball-on

  • how to get file name from URI 2010-11-24

    Hello I am develop video player with android gallery. I receive URI from gallry. and I need to display video title, when play video. so if content has not title meta data. I need to disaplay video file name. How to get video content file name? thank

  • get file path for uri.parse() in android 2011-11-13

    I have the location of a file, D:\Android\WorkSpace\myApp\res\drawable-hdpi\image.PNG how can I get the file path to use inside uri.parse()? --------------Solutions------------- pass the path to your URI path like as follows.may be in two ways: 1.Uri

  • download file from absolute uri to stream to SaveFileDialog 2011-11-22

    I've gotten as far as putting a file into a stream from a url. However puttin savefiledialog inside the event OpenReadCompleted gives an exception because the savefiledialog needs to be fired from an user iniated event. Putting the savefiledialog NOT

  • How to compress picture and create File from picture Uri 2011-12-29

    I have Uri of some image at my external storage but it is too large. I have constructor further which accepts only File like parameter. How to make picture smaller adn get File from that ? --------------Solutions------------- Well that depends on how

  • How to get the file path from URI? 2012-01-16

    This question already has an answer here: Get filename and path from uri from mediastore 10 answers Please find my code below. I need to get the file path of the pdf document, selected by the user from SDcard. The issue is that the URI.getPath() retu

  • Copying files using pathnames in a text file 2013-08-31

    I have a text file containing the full pathnames to selected files in various directories. I wish to copy the these selected files (only the files not the directory structure) to a new directory. I need to do so using XP Dos batch commands (without k

  • Attach File Through mailto URI 2010-03-03

    Is it possible to attach a file using "mailto:" redirect in javascript? Something like this: document.location = "mailto:"+recipient+"?subject="+subject+"&body="+msg+"?attach="+"file1.zip"; --------------Solutions------------- No, you can not add

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.354 (s). 13 q(s)