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

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:

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.

Attachments moved away from Item after validation and before submit process in Apex

I have multiple File Browser Item fields on one page of Application in Oracle Apex.
What happens: When I miss any Item for which validation error fires, I want to hold that file to the browser but I usually loose it if I get that validation error. Is there a solution for the same like other Items fields hold previous value except File Browser Item field. Please see below ss:
Anshul,
APEX 4.2 is very old and no longer supported. A later (or preferably latest) version of APEX will behave differently as Dan explained above.
Can you import your application into apex.oracle.com (which is running APEX 20.1) and you will probably see better results. Based on this you can hopefully use it as justification to upgrade your environment.
Regards,
David
Go to your page-level attributes and a function like the following in the Function and Global Variable Declaration:
function validateItems(request) {
var $file1 = $('#P68_FILE_1');
var $file2 = $('#P68_FILE_2');
var errorsFound = false;
if ($file1.val() === '') {
errorsFound = true;
// Show item in error state
}
if ($file2.val() === '') {
errorsFound = true;
// Show item in error state
}
if (!errorsFound) {
// I think doSubmit was the name of the function back then. If not, try apex.submit
doSubmit(request);
} else {
// Show error message at top of page, I'll use a generic alert for now
alert('You must select a file for each file selector.');
}
}
Then, right-click the Create button and select Create a Dynamic Action. Set the name of the Dynamic Action to Create button clicked.
For the Action, set Type to Execute JavaScript Code. Enter the following JS in code:
validateItems('CREATE');
Finally, ensure that Fire on Initialization is disabled.
Repeat the process for the Save button, but change the request value passed to validateItems to SAVE.

How to create an ImageView as drag and drop?

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.

Unchecking checkbox in oracle apex

Created an report with checkbox using apex_item and when checked more than one check box i will display alert message "not to check more than one checkbox with ok button " after clicking ok it should be unchecked . please find my JavaScript code that displays alert message
if($("input[type=checkbox]:checked").length > 1)
{
var msg = alert('You are not allowed to select more than one employee');
}
It's best to use the APEX JavaScript APIs for this type of thing. You can find them here: https://apex.oracle.com/jsapi
If you're getting started with JavaScript and APEX, you may find these slides useful: https://www.slideshare.net/DanielMcGhan/getting-started-with-javascript-for-apex-developers
Here's a solution that should work for you (just change the name of the item to match yours):
var cbItem = apex.item('P1_CHECKBOX');
if (cbItem.getValue().length > 1) {
alert('You are not allowed to select more than one employee');
cbItem.setValue(); // Passing nothing to clear the value
}

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