How to create an ImageView as drag and drop? - applescript-objc

I would like to know how to make an ImageView drag and drop,
I will drag a certain file to an ImageView and it will render
an action.
I know I can use the code below in a Script Editor and save it as a
application could then drag the files into it and do an action but would like to
know how it would be done in an ImageView.
Thanks in advance!
on open myItems
repeat with one_item in myItems
---Blablabla
end repeat
end open

An NSImageView's parent class is NSControl, so like any other control it has a target/action you can specify, for example:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
property mainWindow : missing value
on run -- Script Editor example
my performSelectorOnMainThread:"doWindowStuff" withObject:(missing value) waitUntilDone:true
end run
on doWindowStuff() -- window stuff needs to be done on the main thread
try
set my mainWindow to makeWindow out of {{0, 0}, {400, 200}} given title:"Drag an image:"
set imageView to makeImageView out of {{100, 50}, {200, 100}}
mainWindow's contentView's addSubview:imageView
mainWindow's makeKeyAndOrderFront:me
on error errmess
display alert "Error" message errmess
end try
end doWindowStuff
to makeWindow out of frame given title:title : "" -- make and return a window
tell (current application's NSWindow's alloc()'s initWithContentRect:frame styleMask:7 backing:2 defer:yes) -- styleMask of 7 includes title, close and minimize buttons
if first item of frame is {0, 0} then tell it to |center|()
its setAutorecalculatesKeyViewLoop:true
its setTitle:title
return it
end tell
end makeWindow
to makeImageView out of frame -- make and return an image view
tell (current application's NSImageView's alloc()'s initWithFrame:frame)
its setTarget:me
its setAction:"imageDragged:"
its setEditable:true
its setImageScaling:(current application's NSImageScaleProportionallyUpOrDown)
return it
end tell
end makeImageView
on imageDragged:sender
activate me
display dialog "An image was dragged into the image view."
end imageDragged:
To do something like get the file path for a dropped image, NSImageView will need to be subclassed. For an Xcode project, add a new .applescript file for the subclass, and set it as the class of your imageView in IB's Identity Inspector:
script MyImageView -- NSImageView subclass
property parent : class "NSImageView"
property imagePath : missing value
on draggingEntered:sender
# additional stuff as desired
return current application's NSDragOperationCopy
end draggingEntered:
on performDragOperation:sender
set pasteboard to sender's draggingPasteboard()
set theOptions to {NSPasteboardURLReadingFileURLsOnlyKey:1}
set imageURLs to pasteboard's readObjectsForClasses:{current application's |NSURL|} options:theOptions
set draggedURL to first item of (imageURLs as list)
set my imagePath to draggedURL as text
return true -- data accepted
end performDragOperation:
end script
You should also register the acceptable drag type(s) for your imageView somewhere in the AppDelegate's setup:
imageView's registerForDraggedTypes:{"public.image"}
The file path will then be available in the imageView's imagePath property when you drag something to the view. See Apple's Drag and Drop Programming Topics for more information.

Related

How do I add validation to text area in oracle apex

I am feed up with this Oracle APEX application , their is no proper videos or documentation
I have a Text area field and button
On button click I have written a PL/SQL code to send mail
Is their any way I can populate error / success message on screen
If text area is empty does not have any data then display error message on screen as Text area field is empty
My code :
BEGIN
if (:textarea1 is not null ) then
APEX_MAIL.SEND(p_from => 'alice#example.com',
p_to => 'bob#example.com',
p_subj => 'Email from Oracle Autonomous Database',
p_body => 'Sent using APEX_MAIL');
else
--popup error message on my screen -> `textarea` filed cannot be empty
end if;
END;
The following package of ADD_ERROR.ADD_ERROR does not exist at my end
If text area is empty does not have any data then display error message on screen
You don't need your own validation for that; just set that item's Value required property ON.
Additionally, you can even set process' Server side condition to
When button pressed: pick the button you use to send an e-mail
Type: Item is not null; pick the text area item
You commented that it isn't working. Well, it is. If it doesn't for you, then you did something wrong (although, I can't imagine what would that be, regarding the fact that it is just one property you had to set).
So: if I leave the text area empty and push the button, error message is here:
As of a video with demonstration: I don't have any. Google for it, if you desperately need it.

How to get the return value of a clicked button using NSAlert

I am using the following:
tell current application's NSAlert's alloc's init()
its setMessageText:"Alert test"
its setInformativeText:"This is a test"
its setAlertStyle:2
its setShowsSuppressionButton:true
its addButtonWithTitle:"Cancel"
its addButtonWithTitle:"Replace"
its beginSheetModalForWindow:theWindow modalDelegate:me didEndSelector:(missing value) contextInfo:(missing value)
end tell
I would like to know how do I get the value of the clicked button including "Suppression Button".
thanks advance!
Using the NSAlert+MyriadHelpers category from Myriad Helpers, you can use the showOverWithSuppress:calling: method. Using Xcode’s default AppleScript application project and adding the category .h and .m files to it, an example would be something like:
property suppressed : false
on applicationWillFinishLaunching:aNotification
tell current application's NSUserDefaults's standardUserDefaults
its registerDefaults:{SuppressAlert:suppressed}
set my suppressed to its objectForKey:"SuppressAlert"
end tell
set state to ""
if not suppressed then set state to "not "
set response to (display dialog "Example alert is currently " & state & "suppressed." buttons {"Clear", "Set", "Continue"} default button 3)
if button returned of response is "Set" then
set my suppressed to true
else if button returned of response is "Clear" then
set my suppressed to false
end if
doAlert()
end applicationWillFinishLaunching:
on doAlert()
log "performing doAlert() handler"
if suppressed then return -- skip it
tell current application's NSAlert's alloc's init()
its setMessageText:"Alert test"
its setInformativeText:"This is a test"
its setAlertStyle:2
its setShowsSuppressionButton:true
its addButtonWithTitle:"Cancel"
its addButtonWithTitle:"Replace"
its showOverWithSuppress:theWindow calling:"alertDidEnd:"
end tell
end doAlert
on alertDidEnd:response
set buttonName to (first item of response) as text
if buttonName = "Cancel" then display alert "Cancel button clicked!"
set my suppressed to (second item of response) as boolean
end alertDidEnd:
on applicationShouldTerminate:sender
tell current application's NSUserDefaults's standardUserDefaults
its setObject:suppressed forKey:"SuppressAlert" — update
end tell
return current application's NSTerminateNow
end applicationShouldTerminate:

Behavioral discrepancy in Tkinter listbox, arrow keys vs. mouse click

My environment is Python 2.7, running on Windows 7.
I'm trying get a Tkinter Listbox to trigger a callback in response to the user changing the 'active' item (i.e. the item with focus). I'm using a binding to the <<ListboxSelect>> event to make this happen, and it's working -- sort of.
The callback itself is supposed to check what the new active item is, and carry out some processing accordingly. This logic operates the way I expect when I change the active item via the up/down arrow keys. But when I point & click on a new item instead, the code mistakenly identifies the prior active item as the current one.
Here's a stripped-down code sample that illustrates the behavior I'm getting:
import Tkinter as tk
#Root window
root = tk.Tk()
#Callback to show focus change
def updateDisplay(*args):
focusIndex = str(lb.index(tk.ACTIVE))
ctrlFI.set('Focus is at index '+focusIndex)
#Control variables
ctrlLB = tk.StringVar()
ctrlFI = tk.StringVar()
#Widgets
lb = tk.Listbox(root,
width=20, height=10,
relief=tk.FLAT,highlightthickness=0,
selectmode=tk.EXTENDED,
activestyle='dotbox',
listvariable=ctrlLB)
lbl = tk.Label(root,
justify=tk.LEFT, anchor=tk.W,
textvariable=ctrlFI)
lb.grid(row=0,column=0,sticky=tk.NW,padx=(5,0),pady=5)
lbl.grid(row=1,column=0,columnspan=2,sticky=tk.NW,padx=5,pady=5)
#Listbox binding to trigger callback
lb.bind('<<ListboxSelect>>',updateDisplay)
#Initializations to prep GUI
ctrlLB.set('Index0-entry Index1-entry Index2-entry Index3-entry Index4-entry')
ctrlFI.set('Ready')
#Begin app
tk.mainloop()
Here are the results when you use the arrow keys:
But here's what you get when you click with the mouse:
The information 'lags' one behind, showing the prior selection instead. (If you click the same item a second time, it 'catches up.')
So my questions are:
What is causing the discrepancy?
How do I fix it so the mouse click gives the right result?
The active item is not necessarily the same as the selected item. When you press the mouse down it changes the selected value but it does not change the active item. The active item only changes once you release the mouse button.
You should be able to see this by clicking and holding the mouse button over an item that is not currently selected. When you do, you'll see something like this:
In the above image, the active item is the one surrounded by a dotted outline. The selected item is in blue. When your code displays the 'focus', it's displaying the active element rather than the selected element.
If you want the selected item, you need to use curselection to get the index of the selected item. It returns a tuple, so in extended mode you need to get the first element that is returned (eg: lb.curselection()[0]). Be sure to handle the case where curselection returns an empty string.

How to maintain variable type when using a function in applescript

I'm writing a script that will constantly scan iTunes for new dialog boxes and close them correctly depending on what they do. For the most part my script is working correctly with the exception that it doesn't search the buttons of the dialog box.
So far the script will wait until another process opens iTunes, and then wait for the first dialog box to appear (using spin-wait loops). Once a dialog box appears it gets the window and then the window's buttons. I would like it to then make decisions based on what the buttons are, but I'm having a bad time with finding out what the buttons are. Below is the entire script:
#repeat
set windowOpen to false
tell application "System Events"
repeat until windowOpen
if window 1 of process "iTunes" exists then
set windowOpen to true
end if
end repeat
set windowOpen to false
tell process "iTunes"
set frontmost to true
set wantedWindow to null
repeat until windowOpen
try
set wantedWindow to first UI element whose role description is "dialog"
set windowOpen to true
on error erMessg
set windowOpen to false
end try
end repeat
set buttonList to every button in wantedWindow
if (count of buttonList) is 1 then
if title of item 1 of buttonList is not "Stop" then
click item 1 of buttonList
end if
else
if my windowContainsButton(buttonList, "Cancel") then
say "Cancel"
end if
# repeat with theButton in buttonList
# if title of theButton is "Cancel" or title of theButton is "Restore" then
# say "cancel"
# delay 1
# end if
# end repeat
end if
end tell
set wantedWindow to null
end tell
#end repeat
on windowContainsButton(listOfButtons, searchFor)
repeat with theButton in listOfButtons
if title of theButton is searchFor then
return true
end if
end repeat
return false
end windowContainsButton
So far, I'm trying to find out if it has found a cancel button by having it say "Cancel". Instead, it's coming up with an error: System Events got an error: Can't make |title| of button "Cancel" of window 1 of application process "iTunes" into type reference. It then points me to my function windowContainsButton on this line:
if title of theButton is searchFor then
and it highlights searchFor.
The windowContainsButton function is exactly the commented out section of code, just generalized. The commented out section works, which is a large part of the reason I'm asking about types.
First off, how can I go about implementing a function like this? Lets say that I actually wanted this function to work, how could I make it?
Secondly, is there a better way to do this? I don't mean the entire script (although I wouldn't doubt that it could be done better), I mean searching the buttons for a particular button that I'm expecting.
edit: another thing I noticed is that "title" is a reserved word in the body of the script, but a variable in the function. I'm used to other languages where reserved words are reserved universally, so I would also like some guidance with what's going on there.
I have not gone through the whole script but this should fix your handler:
on windowContainsButton(listOfButtons, searchFor)
repeat with theButton in listOfButtons
tell application "System Events"
if title of theButton is searchFor then return true
end tell
end repeat
return false
end windowContainsButton
This is a bit cleaner:
property theSeconds : 1
tell application "System Events"
repeat until window 1 of process "iTunes" exists
delay theSeconds
end repeat
tell process "iTunes"
set frontmost to true
repeat until exists (first UI element whose role description is "dialog")
delay theSeconds
end repeat
set wantedWindow to first UI element whose role description is "dialog"
tell wantedWindow
set buttonList to title of every button in wantedWindow
if (count of buttons) is 1 and title of button 1 is not "Stop" then
click button 1
else if buttonList contains "Cancel" then
say "Cancel"
end if
end tell
end tell
end tell

How to load ActivityIndicatorView on TableView inside TabBarcontroller, XCode 4.2

Xcode - version 4.2
Target Platform - iPhone
I have a TabBarController , containing 3 tabs. All tabs contain TableViewController wired to the TabBarController via NavigationController. All TableViewController shows data from external URL, and as well have their corresponding DetailViews.
The application works all fine.
I'm unable to add an 'UIActivityIndicatorView" while the data on the TableView is being loaded. The 'storyboard' neither allows me to add the 'UIActivityIndicatorView' control on the TableViewController or UINavigationController.
Note :
I understand placing UIActivityIndicatorView directly on a UIViewController.
I dont want the UIActivityIndicatorView 'loading' for the cells in the TableView.
Thanks,
Sriram
I'm trying to do the same with Storyboard but it seems that you should bette do it programmatically :
In your ViewController.h :
#property (strong) UIActivityIndicatorView *activityIndicator;
In viewDidLoad :
// init with desired state
activityIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
// set the color
activityIndicator.color = [UIColor blackColor];
activityIndicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
// set the position
activityIndicator.center = CGPointMake(self.view.bounds.size.width/2,
self.view.bounds.size.height/4);
If you have to handle orientation change, think to update the position in viewWillAppear and willRotateToInterfaceOrientation
I think I'll loose less time to code that than time to find a better way to work with Storyboards