Thursday, November 23, 2017

#1 2016-07-19 08:13:41 pm

JosephSimpson
Member
Registered: 2016-07-15
Posts: 2

Objective C option parameter problem

While exploring Applescript Objective C (thanks Shane Stanley), I have found that a large number of Objective C methods have an option parameter. I have no problem supplying one of the option constants, but have been unable to find a way of doing a bitwise OR with multiple constants using applescript.

Does anyone know a way to do this?

Example handler:

Applescript:

on replaceItemAtPath:psxDestPathString withItemAtURL:psxSrcPathString
   -- This handler moves Src contents, replacing Dest contents, keeping Dest name, with backup of Dest. Src path is destroyed.
   
   -- uses the following convenience properties
   -- property thisapp : current application
   -- property NSString : class "NSString"
   -- property NSURL : class "NSURL"
   -- property NSFileManager : class "NSFileManager"

   -- References
   -- NSFileManager instance method
   -- - replaceItemAtURL:withItemAtURL:backupItemName:options:resultingItemURL:error:
   -- NSURL class method
   -- + (NSURL *)fileURLWithPath:(NSString *)path
   
   -- Shove all the pieces into obj-C scope
   set buNameString to (NSString's stringWithString:"Backup.bu")
   set psxDestPathString to (NSString's stringWithString:psxDestPathString)
   set psxSrcPathString to (NSString's stringWithString:psxSrcPathString)
   
   -- Expand paths
   set psxDestPathString to (psxDestPathString's stringByExpandingTildeInPath)
   set psxSrcPathString to (psxSrcPathString's stringByExpandingTildeInPath)
   
   -- Convert NSStrings to NSURLs
   set psxDestPathURL to NSURL's fileURLWithPath:psxDestPathString
   set psxSrcPathURL to NSURL's fileURLWithPath:psxSrcPathString
   
   -- option 1: thisapp's NSFileManagerItemReplacementUsingNewMetadataOnly
   -- option 2: thisapp's NSFileManagerItemReplacementWithoutDeletingBackupItem

   -- Can't figure this out: set bitwiseORComponents to (option 1 | option 2) <------------------------------<<

   -- works fine with one constant
   set replaceOptions to thisapp's NSFileManagerItemReplacementWithoutDeletingBackupItem

   set response to (NSFileManager's defaultManager's replaceItemAtURL:psxDestPathURL withItemAtURL:psxSrcPathURL backupItemName:buNameString options:replaceOptions resultingItemURL:psxDestPathURL |error|:(missing value))
   return response
end replaceItemAtPath:withItemAtURL:

Offline

 

#2 2016-07-19 11:23:11 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11482
Website

Re: Objective C option parameter problem

Hi,

these option constants are just integer values,

NSFileManagerItemReplacementUsingNewMetadataOnly is 1
NSFileManagerItemReplacementWithoutDeletingBackupItem is 2

So simply add them

Applescript:

set replaceOptions to (thisapp's NSFileManagerItemReplacementUsingNewMetadataOnly as integer) + (thisapp's NSFileManagerItemReplacementWithoutDeletingBackupItem as integer)

Browser: Safari 601.3.9
Operating System: Mac OS X (10.10)


regards

Stefan

Offline

 

#3 2016-07-20 01:15:28 am

JosephSimpson
Member
Registered: 2016-07-15
Posts: 2

Re: Objective C option parameter problem

Thanks, Stefan.

I was trying to make it more difficult than necessary.
I realize now that options in objective c methods are simple enumerations which are bit masks with each enumeration shifted left bitwise.

Regards

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)