Re: Trying to move away from Finder...
Re: Trying to move away from Finder...
- Subject: Re: Trying to move away from Finder...
- From: Jean-Christophe Helary <email@hidden>
- Date: Thu, 2 Nov 2017 13:50:29 +0900
Thank you Shane.
> On Nov 1, 2017, at 21:38, Shane Stanley <email@hidden> wrote:
>
>> I have a Finder window opened on a "job" in which there is a folder
>> (originaux) that contains files.
>> I'm creating a folder hierarchy inside that "job" and I want to copy files
>> that are in "originaux" to one of its folders.
>
> If you're happy to do it in AppleScriptObjC, this should give you most of
> what you need. It deals in URLs; it could be done with POSIX paths, but URLs
> often end up more convenient in the long run. The various method names should
> make it reasonably obvious what they do.
Trying to understand the code below (while trying to understand the Foundation
documentation in Xcode):
> use AppleScript version "2.4" -- Yosemite (10.10) or later
> use framework "Foundation"
> use scripting additions
>
> tell application "Finder" to set default_location to POSIX path of (target of
> front window as alias)
> -- make URLs
> set default_location_url to current application's |NSURL|'s
> fileURLWithPath:default_location
It looks like you are calling a handler (fileURLWithPath) that belongs to
whatever |NSURL| is (are the || required ?) and that "|NSURL|" belongs to
"current application", with the parameter default_location.
Checking the Foundation documentation, I find that fileURLWithPath is a "Type
Method" for the "NSURL" class. Is it the same "|NSURL|" as above?
So I guess I can use other "Type Methods" instead of fileURLWithPath to produce
different results, right? As long as the parameter type corresponds to what the
method requests (here, fileURLWithPath requests a "path" and I'm guessing the
only valid path for a NSURL parameter is a POSIX path).
Why call "current application" here ? What is its role here ?
> set originals_folder_url to default_location_url's
> URLByAppendingPathComponent:"originaux"
> set new_folder_url to default_location_url's
> URLByAppendingPathComponent:"whatever"
Once you've set default_location_url to the above thing that I'm not sure I
understand, it looks like it has the ability to be called through handlers,
like "URLByAppendingPathComponent", which is an "Instance Method" in the
Foundation documentation.
Does that mean that the "set default_location_url to" command created an
instance of NSURL and that I can use relevant instance methods on it?
The Foundation documentation also mentions
"URLByAppendingPathComponent:isDirectory:
<apple-reference-documentation://hc_bnxUkO4>" but even though we're creating a
directory, why don't you use it?
I know I should not have thrown away my Objective-C book. I can't find a page
that explains the difference between Instance Method, Type Method (and there is
seemingly also a Class Method thing)...
> -- make new folder
> set fileManager to current application's NSFileManager's defaultManager()
Ok, now I guessed that "NSFileManager" was a class, and I was right... Good :-)
But why not use the "|NSFileManager|" syntax that you used above?
Now, "defaultManager()" looks like it won't take a parameter, but the
Foundation documentation says that defaultManager is a "Type Property", so I'm
not sure what to do with that information and how to interpret what the code
does.
But in the end, we get an instance of the class NSFileManager's in fileManager,
correct? So we can use the instance method "createDirectoryAtURL" on it. The
Foundation documentation shows something like:
createDirectoryAtURL:withIntermediateDirectories:attributes:error:
And the way you write it shows how to make sense of that notation:
> set {theResult, theError} to fileManager's
> createDirectoryAtURL:new_folder_url withIntermediateDirectories:true
> attributes:(missing value) |error|:(reference)
> if theResult as boolean is false then error theError's localizedDescription()
> as text
Here again, you use a " | ... | " notation, for "|error|" this time. Why?
Checking localizedDescription, I see that it is an Instance Property, so I
guess it means it the property of an instance of the class NSError, that is
automatically returned by createDirectoryAtURL and set to theError.
I'm not sure what to do with (missing value) and (reference) though. Is
(missing value) just a way to say that you don't set that parameter? The
documentation for (reference) as a value for |error| is much less clear. I
mean, not clear at all.
createDirectoryAtURL returns YES or NO depending on whether the directory was
created or not, so I guess YES and NO are automatically considered as boolean?
I was not aware of the Applescript error statement (except in "on error"), so
here theError's localizedDescription() returns something that needs to be
changed to text so that error can display it.
> -- get contents of originaux
> set contents_urls to fileManager's
> contentsOfDirectoryAtURL:originals_folder_url includingPropertiesForKeys:{}
> options:(current application's NSDirectoryEnumerationSkipsHiddenFiles)
> |error|:(missing value)
contentsOfDirectoryAtURL requires a number of parameters and now we use "{}" to
say something equivalent, I guess to "(missing value)"?
Also, the documentation for the options is not super clear. Not sure what a
"shallow enumeration" is...
And I guess we are not interested in the error message here...
> -- move the files
> repeat with one_url in contents_urls
> set destination_url to (new_folder_url's
> URLByAppendingPathComponent:(one_url's lastPathComponent()))
> (fileManager's moveItemAtURL:one_url toURL:destination_url
> |error|:(missing value))
> end repeat
It's slowly beginning to make sense... Plus we have a super sunny afternoon
here, so I guess I'm blessed...
Ok, here we use URLByAppendingPathComponent again, this time by adding not an
arbitrary string like we did when we created new_folder_url's but by adding the
lastPathComponent() of the URL of one of the contents of the "originaux" folder.
lastPathComponent is an Instance Property. It looks like the syntax for all the
properties we've had so far was property() and not property.
Now we have a (fileManager's moveItemAtURL:one_url...), why is it between
parens and not on it's own like a normal statement?
I guess that's all for the time being... Sorry for the number of questions and
comments, etc.
Also, I do have your book on ASOC, so if answers are to be found there (sorry I
was focusing on the Foundation doc here), don't hesitate to give a pointer
instead.
Jean-Christophe Helary
-----------------------------------------------
@brandelune http://mac4translators.blogspot.com
_______________________________________________
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