• 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
Re: Does a NSURL Have a Valid Target
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Does a NSURL Have a Valid Target


  • Subject: Re: Does a NSURL Have a Valid Target
  • From: Jim Underwood <email@hidden>
  • Date: Thu, 02 Mar 2017 19:32:00 +0000
  • Thread-topic: Does a NSURL Have a Valid Target

Chris and Shane, thanks for sharing this great little script.

I have made a small mod to refactor into a handler, and add support for a tilde path:

doesItemExist(pPOSIXPath)



-------------------------------------------------------------------------------------------

# Auth: Christopher Stone { Heavy Lifting by Shane Stanley }

# Mod:  @JMichaelTX (refactor)  2017-03-02

# dCre: 2017/03/02 11:31

# dMod: 2017/03/02 11:33

# Appl: AppleScriptObjC

# Task: Determine whether a NSURL has a valid target.

# Libs: None

# Osax: None

# Tags: @Applescript, @Script, @ASObjC, @Test, @NSURL, @Valid, @Target

-------------------------------------------------------------------------------------------

use AppleScript version "2.4" -- Yosemite (10.10) or later -- I'm not sure about os versions...

use framework "Foundation"

use scripting additions

-------------------------------------------------------------------------------------------


set folderPathExisting to POSIX path of (path to downloads folder)

set folderPathNotExisting to POSIX path of (path to downloads folder) & "Nuts!"


## @JMichaelTX:  My Test Cases


set goodPathBool to my doesItemExist(folderPathExisting)

set badPathBool to my doesItemExist(folderPathNotExisting)

set invalidPathBool to my doesItemExist("not a valid path")

set pathWithTidleBool to my doesItemExist("~/Downloads")


-------------------------------------------------------------------------------------------


--

## Refactor into Handler by @JMichaelTX


on doesItemExist(pPOSIXPath)

  local myNSURL, doesItExistBool

  

  ## @JMichaelTX:  Add support for tidle (~) Path

  

  set pPOSIXPath to (current application's NSString's stringWithString:pPOSIXPath)'s stringByExpandingTildeInPath

  

  set myNSURL to current application's |NSURL|'s fileURLWithPath:pPOSIXPath

  set doesItExistBool to (myNSURL's checkResourceIsReachableAndReturnError:(missing value)) as boolean

  

  return doesItExistBool

end doesItemExist



RESULTS:

(*goodPathBool: true*)
(*badPathBool: false*)
(*invalidPathBool: false*)
(*pathWithTidleBool: true*)







Best Regards,

Jim Underwood
aka JMichaelTX


From: <applescript-users-bounces+jmichael=email@hidden> on behalf of Chris Stone <email@hidden>
Date: Thu, Mar 2, 2017 at 12:47 PM
To: "ASUL (AppleScript)" <email@hidden>
Subject: Does a NSURL Have a Valid Target

Hey Folks,

Shane gave me another useful tidbit yesterday.

checkResourceIsReachableAndReturnError

-------------------------------------------------------------------------------------------
# Auth: Christopher Stone { Heavy Lifting by Shane Stanley }
# dCre: 2017/03/02 11:31
# dMod: 2017/03/02 11:33
# Appl: AppleScriptObjC
# Task: Determine whether a NSURL has a valid target.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @Test, @NSURL, @Valid, @Target
-------------------------------------------------------------------------------------------
useAppleScriptversion"2.4" -- Yosemite (10.10) or later -- I'm not sure about os versions...
useframework "Foundation"
usescripting additions
-------------------------------------------------------------------------------------------

setfolderPathExisting toPOSIX pathof (path todownloads folder)
setfolderPathNotExisting toPOSIX pathof (path todownloads folder) & "Nuts!"

settheNSURL tocurrent application's|NSURL|'s fileURLWithPath:folderPathExisting

setfolderPathNotExistingBool to (theNSURL'scheckResourceIsReachableAndReturnError:(missing value)) asboolean

settheNSURL tocurrent application's|NSURL|'s fileURLWithPath:folderPathNotExisting

setfolderPathExistingBool to (theNSURL'scheckResourceIsReachableAndReturnError:(missing value)) asboolean

-------------------------------------------------------------------------------------------

--
Best Regards,
Chris

 _______________________________________________
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: Does a NSURL Have a Valid Target
      • From: Jim Underwood <email@hidden>
References: 
 >Does a NSURL Have a Valid Target (From: Christopher Stone <email@hidden>)

  • Prev by Date: Does a NSURL Have a Valid Target
  • Next by Date: Re: Does a NSURL Have a Valid Target
  • Previous by thread: Does a NSURL Have a Valid Target
  • Next by thread: Re: Does a NSURL Have a Valid Target
  • Index(es):
    • Date
    • Thread