Showing a popover underneath a piece of selected text is a trick widely made use of across OSX, not least in Xcode itself, but also in many other apps. In this post we’re going to look at a simple way to achieve this effect.
1. First of all, in IB drag out a Popover and View Controller and drop them in the Document Outline area. Drag a custom view to the canvas, and hook them all up thus:
delegate –> App Delegate (or class that will control the Popover)
Popover View Controller’s Outlets
view –> Custom View
Click the Popover object in the Outline area, and in the Attributes Inspector, set the Behaviour to Transient.
In the appDelegate.h file, or the .h file of the class that will control your popover, include the PopoverDelegate in the @interface declaration:
@interface AppDelegate : NSObject <NSApplicationDelegate, NSPopoverDelegate>
2. Still in the header file, you need to make sure the window that the NSTextView is in has a View outlet in its header file.
@property (weak) IBOutlet NSView *aView;
For my purposes, I just have a single window in the appDelegate class, so I just created a view property by control-dragging out of the Window’s view object to the header file. You’ll need to switch to the dual-view Assistant editor to do this:
While you’ve got the Assistant editor open, drag out an outlet from the Popover to the .h file and name it ‘popover’. Finally, in the same way create a similar outlet for your TextView.
4. Next, go into the implementation .m file for the appDelegate (or you class). You’ll need an IBAction to trigger the showing of the popover. In my case, I have an ‘Enter’ button the user hits after making a selection attached to a method I called enterSelection:(NSButton *)sender.
In this method, I first get the rect for the user’s selection with:
NSRect rect = [_textView firstRectForCharacterRange:[_textView selectedRange] actualRange:NULL];
That will return a rect in screen coordinates for the selected text. However, I need to convert that into the window’s coordinates with:
NSRect converted = [_window convertRectFromScreen:rect];
Now we’re ready to call the popover by supplying this rect to the first parameter and the View property we created earlier to the second parameter:
[self.popover showRelativeToRect:converted ofView: _aView preferredEdge:NSMinYEdge];
And that’s it. Your popover should show underneath the selected text whenever your method gets called. If you want to see how this is done step by step in Xcode, check out the video:
OSXClock just got a major update, adding a productivity log that helps you to track how much time you spend actively using each app on your mac.
I wrote an ad-hoc AppleScript to do this sometime ago that proved pretty popular, but I wasn’t satisfied with either the code or the interface. OSXClock improves on that by tapping directly into Cocoa’s API and by offering a more attractive display.
OSXClock is currently on offer for only $2.99. Lots more exciting features are planned for future updates, so now’s a good time to get with the program, folks! :)
*OSXClock requires OSX Yosemite
Xcode being the vast IDE that it is, it’s sometimes the simplest things that flummox you. It’s rare that I ever want to look at the XML code for any of my interface files, unless I’m copying one from one project to another or hunting down some forgotten outlet that’s throwing a warning. But when I do, I invariably forget how to get back to IB view.
If that’s you, fortunately it’s easy to return to the Interface Builder view from the source code view. Just right-click (aka ‘Control click’) on your nib file in the project navigator sidebar and choose “Open as…” and “Interface Builder XIB Document”. Unlike myself, you’ll remember that for next time, too (me, I’ll be looking for this post again in six months time! :) ).
Well, Tim Cook says sitting is the new cancer…and since I don’t envisage myself in the market for an iWatch, but do need to be reminded to get up and take a break from the desk every 60 minutes, I wrote OSXClock.
I also need a clock for displaying the time on a large screen on occasion when conducting timed-based tests. In the past, I’ve used timeanddate.com for this, but it has a couple of disadvantages: first, you have to be connected to the internet; second, it doesn’t have an alarm or timer.
Hence, OSXClock was born, Applehelpwriter’s answer to the Apple Watch :p, and considerably cheaper! :D
Here’s a short video showing some of the differences between Apple’s own Script Editor and my DisplayDroid.
If you haven’t got 5 minutes, the highlights include:
DisplayDroid shows result of each line of the script
DisplayDroid offers more informative error messages
DisplayDroid allows you to set a breakpoint on any line in your script
DisplayDroid lets you step through the script line by line
Full details of DisplayDroid can be found here: http://sqwarq.com/displaydroid/ To get a free beta copy of the app, please register for the Community forum here: http://displaydroid.proboards.com and I will email you a beta copy of the app in the next few days.
Ever since Apple introduced Notification Centre, scripting it has been anything but easy.
I’ve seen brutal shell scripts and a variety of GUI scripts that variously work in Mountain Lion and Mavericks to turn ‘Do Not Disturb’ on and off. With Yosemite, Apple made yet another change to the ui process that controls Notification Centre, which means scripts like this one will choke.
If you’re wondering how to simply toggle whether Notification Centre is enabled or not with AppleScript on Yosemite, here’s the trick (whether this will continue to work in 10.11 is anyone’s guess). Enjoy it while it lasts! :)
tell application "System Events"
tell application process "SystemUIServer"
if exists menu bar item "NotificationCenter, Do Not Disturb enabled" of menu bar 2 then
key down option
click menu bar item "NotificationCenter, Do Not Disturb enabled" of menu bar 2
key up option
key down option
click menu bar item "Notification Center" of menu bar 2
key up option
key up option
As I noted in an earlier post, one of Yosemite’s annoying usability regressions is that Apple have removed the Dock Preferences from the menu.
I was so irritated by this that I thought I’d just slip them back in to the menu bar. ;) Hence, FastTasks 2 from v1.6 onwards now lets you manage most Dock preferences from the menu bar again!
Security researchers have this week been getting themselves het up about a new malware threat to both iOS and OS X. WireLurker appears to be emanating out of Chinese file exchange sites and, at least at the moment, looks fairly limited in both its spread and its damage (update: Business Insider is reporting that Apple has blocked WireLurker-infected apps from launching).
However, researchers at Paolo Alto Networks are pointing out that what makes WireLurker particularly worrying is that the malware exploits weaknesses in Apple’s software that could, they claim, be easily be used for far more dangerous threats.
You can easily scan for the malware threat with my free app FastTasks 2 (v 1.53 or later). If you don’t see the warning as in the screenshot above or any results in the Analyser ‘Issues’ pane, you’re clean of any of the currently known files associated with WireLurker. If you do see the warning, locate the infectious files from the Analyser pane and delete (OS X will demand your Admin password to remove some of them), then restart your mac.