Objc scriptingbridge how open new Finder window in a specific folder - objective-c

I want to open a new Finder window of a specific size in a specific position and displaying a specific folder.
In ScriptingBridge (XCode 6, Mavericks 9.4) I can open a new Finder window and have figured out how to read the URL of the folder it opens. But I am really struggling with how to set the folder to something different.
I have tried to assign the window a 'target' SBObject initialised with a 'URL' property.
but the URL property is readonly although 'target' seems not to be. I've used 'get' to ensure there is a FinderFolder object and a FinderFinderWindow object.
I've tried using a dictionary of properties with a URL key and creating a 'folder'.
FinderFolder *folder = [[[_finder classForScriptingClass:#"folder"] alloc]
initWithProperties:dict];
[[_finder folders] addObject:folder];
I have read the Apple docs and looked for examples all over but cannot find an instance of someone assigning the folder path.

// open folder "Documents" of home folder
FinderFolder *theHomeFolder = [theFinder home];
NSLog(#"theHomeFolder: %#",theHomeFolder);
SBElementArray * theHomeFolderFolders = [theHomeFolder folders];
NSLog(#"theHomeFolderFolders: %#",theHomeFolderFolders);
FinderFolder *theDocsFolder = [theHomeFolderFolders objectWithName:#"Documents"];
NSLog(#"theDocsFolder: %#",theDocsFolder);
[theDocsFolder openUsing:nil withProperties:nil];
FinderFinderWindow * docsFolderWindow = (FinderFinderWindow*) [theDocsFolder containerWindow];
NSLog(#"docsFolderWindow: %#",docsFolderWindow);
docsFolderWindow = [docsFolderWindow get];
NSLog(#"docsFolderWindow: %#",docsFolderWindow);
// change its bounds
docsFolderWindow.bounds = NSMakeRect(64., 64., 800, 600.);
// other fun stuff…
[docsFolderWindow setCurrentView:FinderEcvwListView];
[[docsFolderWindow iconViewOptions] setArrangement:FinderEarrArrangedByName];
[docsFolderWindow cleanUpBy:#selector(name)];
docsFolderWindow.toolbarVisible = NO;
docsFolderWindow.statusbarVisible = NO;
docsFolderWindow.sidebarWidth = 0;

There are a few different ways. The other answer works, or you could explicitly make a new Finder window and set its target property:
NSURL *u = [NSURL fileURLWithPath:#"/tmp"];
FinderFinderWindow *w = [[[theFinder classForScriptingClass:#"Finder window] alloc] init];
[[theFinder finderWindows] addObject:w];
[w setTarget:u];
You could also set it to an SBObject referring to a Finder item, such as theDocsFolder from the other answer.

Related

Evernote and SBApplication bridge - createNoteWithENML issue - resources added twice

The problem:
//Create Evernote App SBApplication
EvernoteApplication *evernoteApp = [SBApplication applicationWithBundleIdentifier:#"com.evernote.Evernote"];
evernoteApp.delegate = self;
//Take this ENML Code
NSString *enml = #"<div>This is an encrypted text for OneNote Saferoom.</div><div><br/></div><div>PDF file</div><div><en-media type=\"application/pdf\" style=\"cursor:pointer;\" height=\"43\" hash=\"fa7d7e650b2cec68f302b31ba28235d8\"/></div><div><br/></div><div>Image</div><div><en-media style=\"height: auto;\" type=\"image/png\" hash=\"64957a2bd851f625383a5b9e410965d7\"/></div>";
//Create correct Array of URLS for above (Set the URLs before)
NSArray *resourceURLs = [NSArray arrayWithObjects:url2,url1,nil];
//Upload the note to Evernote
[evernoteApp createNoteFromFile:nil fromUrl:nil withText:nil withHtml:nil withEnml:[NSString stringWithFormat:#"<en-note>%#</en-note>",enml] title:noteTitle notebook:workingNotebook tags:tags attachments:resourceURLs created:nil];
And now I get the note with additional duplicated resources (Evernote app just adds all resources from resourceURLs array to the end of the note):
Tried the following
remove resourceURLs array - no resources added
using HTML and adding resource URLs - structure is not kept, resources appear at the end only
converting from ENML to HTML using Inline resources function (ENMLUtility) - resources are not displayed
I am starting to believe that there is a bug in how Evernote.app handles createNote request.
Resources to test
PDF sample file for above resource:
https://www.dropbox.com/s/t8pgzzexms0nl7w/dec-pdf-sample.pdf?dl=0
Image sample
https://www.dropbox.com/s/nagaei1hng6ziwc/dec-touchID_blue_80%403x.png?dl=0
Generated Evernote.h file for bridging:
https://www.dropbox.com/s/7u1zdb8c71ydwag/Evernote.h?dl=0
Any help will be appreciated.
Update 1:
Tested with pure AppleScript, still resources are duplicated:
https://www.dropbox.com/s/rvpc4est8ef7q4c/EvernoteAppleScript_bug.scpt?dl=0
Thanks,
Ilia

Permission to take photo OR get image from library not shown in iOS9 (Xcode 7beta, Swift2)

The code below shows an example for my access to the image lib. No matter where I call the code (view) I do not see the permission dialog from the phone popping up and therefore cannot allow my app to access either camera or library.
Also, the privacy settings do not show my app either. Any thoughts? I'm going nuts.
let imgPicker = UIImagePickerController()
imgPicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
imgPicker.modalPresentationStyle = UIModalPresentationStyle.Popover
self.presentViewController(imgPicker, animated: true, completion: nil)
another way I tried
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) {
let imagePicker:UIImagePickerController = self.imagePickerController
imagePicker.allowsEditing = true
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Photo
imagePicker.cameraDevice = UIImagePickerControllerCameraDevice.Rear
imagePicker.showsCameraControls = true
imagePicker.navigationBarHidden = true
imagePicker.toolbarHidden = true
imagePicker.delegate = self
self.presentViewController(imagePicker, animated: true, completion: nil)
}
You need to set a new pair of Info.plist values, same as the String for Location Services would be in iOS8:
Just set your description string for those.
Check info.plist.
if "Bundle display name" set as nothing,
At iOS 8.1 will popup permission window like this
"" Would Like to Access Your Photos
But at iOS 9, there is no popup permission at all.
Also, no permission setting, at Setting > Privacy > Photos > Your app name.
To solve this,
Remove "Bundle display name" in your info.plist.
Apparently this appears to be an issue with iOS9. For some reason the permission access dialog does not pop up. I have successfully tested on iOS8, using the same code.

How to populate a NSPopUpButton with the user's favorite folders

I am new to programming for Mac, so forgive me if my question is too silly.
I am writing a small application, where I need to set a destination folder.
I think, instead of just using a NSButton "Choose folder", the approach that Firefox or Safari with their "Save as..." dialogs take is a very user friendly one.
Using a NSPopUpButton, where one can pick a folder from the user's favorites, or from the last used folders. Additionally I would add a top-most entry "Choose...", what would open a NSOpenPanel.
My question is: How can I get the user's favorite folders, that are shown e.g. in the Finder application, and populate my NSPopUpButton with them?
Here how it looks like in Firefox:
You can find the relevant functions inside the Application Services framework, and you can get the list of items like this:
LSSharedFileListRef favorites = LSSharedFileListCreate(NULL, kLSSharedFileListFavoriteItems, NULL);
CFArrayRef snapshot = LSSharedFileListCopySnapshot(favorites, NULL);
CFIndex snapshotCount = CFArrayGetCount(snapshot);
for (CFIndex i = 0; i < snapshotCount; ++i) {
LSSharedFileListItemRef item = (LSSharedFileListItemRef)CFArrayGetValueAtIndex(snapshot, i);
CFURLRef itemURL = NULL;
LSSharedFileListItemResolve(item, kLSSharedFileListNoUserInteraction | kLSSharedFileListDoNotMountVolumes, &itemURL, NULL);
NSLog(#"%#", itemURL);
if (itemURL != NULL) {
CFRelease(itemURL);
}
}
CFRelease(snapshot);
CFRelease(favorites);
When I run this on my computer, I get:
nwnode://domain-AirDrop
file://localhost/Applications/
file://localhost/System/Library/CoreServices/Finder.app/Contents/Resources/MyLibraries/myDocuments.cannedSearch/
file://localhost/Users/dave/
file://localhost/Users/dave/Desktop/
file://localhost/Users/dave/Developer/
file://localhost/Users/dave/Documents/
file://localhost/Users/dave/Downloads/
file://localhost/Users/dave/Dropbox/
Which corresponds to:

Adding an item to sidebar by editing com.apple.sidebarlists.plist using objective C

I have an application which has to be shown on Users list of Finder's sidebar on installation.
So on installation code, I have added one more dictionary object to Library -> Preferences-> com.apple.sidebarlists.plist..
i.e., in useritems -> customListItems of plist.
If I see the plist addition everything looks correct.
On relaunching the Finder.app, it is expected to get that item added in the side bar of Finder. But I am not able to see any change happening instead the plist is overridden with the old items. I tried trashing Finder cache and running the code. still no luck :( Any pointers to what I am missing please.
Thanks in advance!
Use LSSharedFileList.
Add an item to the Finder/Save dialog sidebar
-(void) addPathToSharedItem:(NSString *)path
{
CFURLRef url = (CFURLRef)[NSURL fileURLWithPath:path];
// Create a reference to the shared file list.
LSSharedFileListRef favoriteItems = LSSharedFileListCreate(NULL,
kLSSharedFileListFavoriteItems, NULL);
if (favoriteItems) {
//Insert an item to the list.
LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(favoriteItems,
kLSSharedFileListItemLast, NULL, NULL,
url, NULL, NULL);
if (item){
CFRelease(item);
}
}
CFRelease(favoriteItems);
}

How to add a note on UIwebview

I would like to implement a function like adding a note of a selected text on UIWebview as iBooks and Amazon Kindle does.
I already created UIMenuItem. But, I dont know how to implement the method for this. Could anybody help in this regard?
I dont know what functionality is used to implement in this method. Thanks.
- (void)Note:(id)sender {
NSString *selection = [webView stringByEvaluatingJavaScriptFromString:#"window.getSelection().toString()"];
}
The JavaScript in question is designed to give you the text that is highlighted within your selection.
I'm not sure that running toString on an element will return the correct information though. The following method will return the highlighted text and save it into a variable.
function getHighlightedString() {
var text = window.getSelection();
myAnchorOffset = text.anchorOffset;
myFocusOffset = text.focusOffset;
myHighlightLength=myFocusOffset-myAnchorOffset;
if(myHighlightLength<0)
{
myHighlightLength*=-1;
temp = myAnchorOffset;
myAnchorOffset = myFocusOffset;
myFocusOffset = temp;
}
selectedText = text.anchorNode.textContent.substr(myAnchorOffset, myFocusOffset - myAnchorOffset);
}
when you have this method loaded into the webview
NSString myHighlightedText = [webView stringByEvaluatingJavaScriptFromString:#"getHighlightedString()"];