• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Convert File or Path Input into NSURL, HFS path, or POSIX Path Output
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Convert File or Path Input into NSURL, HFS path, or POSIX Path Output


  • Subject: Convert File or Path Input into NSURL, HFS path, or POSIX Path Output
  • From: Christopher Stone <email@hidden>
  • Date: Tue, 28 Feb 2017 14:11:19 -0600

Hey Folks,

I have a vanilla handler that does this and is seriously quick.

But I was writing an ASObjC script that needed the ability and felt bad about using a vanilla handler, when I was sure it could be done without too much trouble in ASObjC.  So I dug out another one of Shane's handlers and didn't have to do it all myself.

This code is also seriously quick.

Thanks Shane!  :)

--
Best Regards,
Chris

-------------------------------------------------------------------------------------------
# Auth: Christopher Stone { Heavy Lifting by Shane Stanley }
# dCre: 2017/02/28 12:00
# dMod: 2017/02/28 12:21
# Appl: AppleScriptObjC
# Task: Convert file or file-path (hfs or posix) into a NSURL – output as NSURL, HFS path, or POSIX Path.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @Convert, @File, @Path, @NSURL, @HFS, @POSIX, @Path, @POSIX_Path
-------------------------------------------------------------------------------------------
use framework "Foundation"
use framework "AppKit"
use scripting additions
-------------------------------------------------------------------------------------------

set theAlias to path to downloads folder
set theFurl to theAlias as «class furl»
set thePosixPath to POSIX path of theAlias
set theTildePath to "~/Downloads/"

set converted_theAlias to its makeURLFromFileOrPath:theAlias
set converted_theFurl to its makeURLFromFileOrPath:theFurl
set converted_tildePath to its makeURLFromFileOrPath:theTildePath
set converted_posixPath to its makeURLFromFileOrPath:thePosixPath

-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
on makeURLFromFileOrPath:theFileOrPathInput
    # Make input into a Cocoa object for easier comparison
    set theFileOrPath to item 1 of (current application's NSArray's arrayWithObject:theFileOrPathInput)

    

    if (theFileOrPath's isKindOfClass:(current application's NSString)) as boolean then

        

        if (theFileOrPath's hasPrefix:"/") as boolean then -- input is full POSIX path
            set myNSURL to current application's class "NSURL"'s fileURLWithPath:theFileOrPath

            

        else if (theFileOrPath's hasPrefix:"~") as boolean then -- input is a POSIX path needing ~ expansion
            set myNSURL to current application's class "NSURL"'s fileURLWithPath:(theFileOrPath's |stringByExpandingTildeInPath|())

            

        else -- input must be an HFS path
            set myNSURL to current application's class "NSURL"'s fileURLWithPath:(POSIX path of theFileOrPathInput)

            

        end if

        

    else if (theFileOrPath's isKindOfClass:(current application's class "NSURL")) as boolean then -- happens with files and aliases in 10.11
        set myNSURL to theFileOrPath

        

    else -- must be a file or alias
        set myNSURL to current application's class "NSURL"'s fileURLWithPath:(POSIX path of theFileOrPathInput)

        

    end if

    

    # return myNSURL
    # return myNSURL as text -- HFS path
    return POSIX path of (myNSURL as text)

    

end makeURLFromFileOrPath:
-------------------------------------------------------------------------------------------

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Convert File or Path Input into NSURL, HFS path, or POSIX Path Output
      • From: Shane Stanley <email@hidden>
  • Prev by Date: Re: Sending mail w/attachment from Terminal
  • Next by Date: Re: Sending mail w/attachment from Terminal
  • Previous by thread: Re: Sending mail w/attachment from Terminal
  • Next by thread: Re: Convert File or Path Input into NSURL, HFS path, or POSIX Path Output
  • Index(es):
    • Date
    • Thread