Thursday, July 9, 2020

#1 2020-05-11 03:09:37 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 280

NSArray, pathComponents

Hi All

I have a bash function that look like this and need little help.

It works like this:
if the path of the directory is Desktop, $up 2, and now the path will be /Users/

I was thinking about doing a handler of it in AppleScriptObjC.

on upPath(thePath,theNumber)
     ... code here
end upPath

function up() {
  local d=""
  limit=$1
  for ((i=1 ; i <= limit ; i++))
    do
      d=$d/..
    done
  d=$(echo $d | sed 's/^\///')
  if [ -z "$d" ]; then
    d=..
  fi
  cd $d
}



I was thinking about doing a AppleScriptObjC of this function after I look at Apple Documents.

https://developer.apple.com/documentati … guage=objc

Here is my code so far... but its does not work.

Applescript:

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

set thePath to POSIX path of (path to desktop as text) & "dummyFile.txt"
set theString to current application's NSString's stringWithString:thePath
log theString's className() as text --> __NSCFString

set pComponents to current application's NSArray's pathComponents:{theString}

error "+[NSArray pathComponents:]: unrecognized selector sent to class 0x7fffa944b228" number -10000



Regards.


I  have skied the highest mountain without logic, and still its the imagination that take me anywhere. Its the desire of something that matters for any knowledge I have or could learn.

Offline

 

#2 2020-05-11 06:04:59 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6397

Re: NSArray, pathComponents

I think the NSString instance method you're looking for is stringByDeletingLastPathComponent().


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#3 2020-05-11 07:30:37 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 280

Re: NSArray, pathComponents

Hi Shane. Thanks, I will check it out...

To be clear, what I trying to do.

if the path is directory path it will separate the string with "/"

So... /disk/users/username/desktop become...

{"disk","users","username","desktop"} >> This is the part I do not get... I thought pathComponents
was the solution from what I read on Apple website.

if the index is 2

it will get items from -1 to -2

and remove it from the list

now we have {"disk","users"}

and build path that become /disk/users


I  have skied the highest mountain without logic, and still its the imagination that take me anywhere. Its the desire of something that matters for any knowledge I have or could learn.

Offline

 

#4 2020-05-11 07:55:52 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 280

Re: NSArray, pathComponents

Shane, its works... I never thought about running the command in repeat.
Apple website gave me that idea after I read the reference they have.

Applescript:

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

set thePath to POSIX path of (path to desktop) & "dummyFile.txt"
set theString to current application's NSString's stringWithString:thePath
log theString's className() as text --> __NSCFString


set theCount to 1
repeat with i from 1 to (theCount + 1)
   set theString to theString's stringByDeletingLastPathComponent()
   log theString as text
   log theString's className() as text --> NSPathStore2
end repeat
return theString as text


I  have skied the highest mountain without logic, and still its the imagination that take me anywhere. Its the desire of something that matters for any knowledge I have or could learn.

Offline

 

#5 2020-05-11 07:56:45 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6397

Re: NSArray, pathComponents

You're better off calling stringByDeletingLastPathComponent() as many times as requested:

Applescript:

on upPath(thePath, theNumber)
   set theString to current application's NSString's stringWithString:thePath
   repeat theNumber times
       set theString to theString's stringByDeletingLastPathComponent()
   end repeat
   return theString as text
end upPath

You can get the components:

Applescript:

set thePath to POSIX path of (path to desktop as text) & "dummyFile.txt"
set theString to current application's NSString's stringWithString:thePath
set theComponents to theString's pathComponents()

But you're better off using the path manipulation methods if possible. Then you don't have to worry about leading or trailing slashes.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#6 2020-05-11 08:12:02 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 280

Re: NSArray, pathComponents

Applescript:

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

log upPath("/Users/Username/Desktop/dummyFile.txt", 0) --> /Users/Username/Desktop
log upPath("/Users/Username/Desktop/dummyFile.txt", 1) --> /Users/Username
log upPath("/Users/Username/Desktop/dummyFile.txt", 2) --> /Users

on upPath(theDir, theIndex)
   set thePath to POSIX path of theDir
   set theString to current application's NSString's stringWithString:thePath
   -- log theString's className() as text --> __NSCFString
   
   repeat with i from 1 to (theIndex + 1)
       set theString to theString's stringByDeletingLastPathComponent()
       -- log theString as text
       -- log theString's className() as text --> NSPathStore2
   end repeat
   return theString as text
end upPath


I  have skied the highest mountain without logic, and still its the imagination that take me anywhere. Its the desire of something that matters for any knowledge I have or could learn.

Offline

 

#7 2020-05-11 08:54:48 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 280

Re: NSArray, pathComponents

I agree with you, your way was much better and now after it make sense.

Also its more close the original code 'bash'...

Thanks so much.


I  have skied the highest mountain without logic, and still its the imagination that take me anywhere. Its the desire of something that matters for any knowledge I have or could learn.

Offline

 

#8 2020-05-11 11:26:11 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 280

Re: NSArray, pathComponents

Shane Stanley wrote:

set thePath to POSIX path of (path to desktop as text) & "dummyFile.txt"
set theString to current application's NSString's stringWithString:thePath
set theComponents to theString's pathComponents()



Shane, The reason why my function of 'pathComponents()' didn't work.
I try to convert this code from Apple reference

NSString *path = @"tmp/scratch";
NSArray *pathComponents = [path pathComponents];



To AppleScriptObjC

My thinking was the first line is a NSString and the second line was NSArray.
But its clear to me now its pathComponents that make the NSString to become NSArray.

There are lot of information about Objective-C but not so much about how translate ObjC to
AppleScriptObjC but your book is a goldmine.


I  have skied the highest mountain without logic, and still its the imagination that take me anywhere. Its the desire of something that matters for any knowledge I have or could learn.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)