如何使用 Applescript 创建 Popover 视图?
How to create a Popover view with Applescript?
我想知道如何通过单击弹出式按钮来显示消息,这里有人可以指导我这样做吗?
例如,单击一个按钮会打开一个 Popover 视图,通知我我的 IP。
NSPopover 只是一个视图,您可以在另一个视图的某个位置显示它,因此您可以从按钮的操作处理程序中显示它。
举个例子,创建任何你想放在弹出框里的东西,比如 NSTextField,然后创建一个大的 NSViewController足以容纳它。接下来,创建 NSPopover,将其 contentViewController
设置为视图控制器,并通过其 showRelativeToRect:ofView:preferredEdge:
在所需位置显示它:
use framework "Cocoa"
property theWindow : missing value -- the main window
on run -- run on main thread if using the Script Editor
if current application's NSThread's isMainThread() as boolean then
doStuff()
else
my performSelectorOnMainThread:"doStuff" withObject:(missing value) waitUntilDone:true
end if
end run
to doStuff() -- make a window to put the example stuff in
set theWindow to current application's NSWindow's alloc's initWithContentRect:{{200, 400}, {200, 100}} styleMask:7 backing:(current application's NSBackingStoreBuffered) defer:true
theWindow's contentView's addSubview:(my makeButtonWithTitle:"Button" atLocation:{60, 40})
theWindow's makeKeyAndOrderFront:me
end doStuff
to makeButtonWithTitle:title atLocation:location -- make a button at {x, y}
tell (current application's NSButton's buttonWithTitle:title target:me action:"buttonAction:") -- 10.12 Sierra and later
its setFrameOrigin:location
its sizeToFit() -- fit to the title
its setRefusesFirstResponder:true -- no highlight
return it
end tell
end makeButtonWithTitle:atLocation:
on buttonAction:sender -- do something when the button is clicked
set title to sender's title
set testString to "This is some popover text
located at " & quoted form of (title as text) & "." -- whatever
set {{x, y}, {width, height}} to (sender's frame()) as list -- for adjusting the location of the pointer
set x to x + (width div 2) -- center horizontally
set y to y + (height div 2) -- center vertically
my showPopoverWithMessage:testString atPoint:{x, y} inView:(sender's |window|'s contentView()) -- use the window the button is in for the view
end buttonAction:
to showPopoverWithMessage:theMessage atPoint:thePoint inView:theView -- build and show the popover
set textField to makeTextField() -- make a text field
textField's setStringValue:theMessage -- put something in it
textField's sizeToFit() -- resize the text field to fit the string
set {width, height} to second item of (textField's frame as list) -- to size the view to the text field
set popoverView to current application's NSView's alloc's initWithFrame:{{0, 0}, {width + 15, height + 15}} -- make a view to hold everything
popoverView's addSubview:textField -- put the text field in it
set popoverViewController to current application's NSViewController's alloc's init -- make a controller for the popover view
popoverViewController's setView:popoverView -- set the view to it
tell current application's NSPopover's alloc's init() -- make the popover
its setContentViewController:popoverViewController
its setBehavior:(current application's NSPopoverBehaviorTransient)
its showRelativeToRect:{thePoint, {1, 1}} ofView:theView preferredEdge:(current application's NSMaxYEdge) -- and show it
end tell
end showPopoverWithMessage:atPoint:inView:
to makeTextField() -- make a text field for the popover
tell (current application's NSTextField's alloc's initWithFrame:{{10, 10}, {100, 100}})
its setBordered:false
its setDrawsBackground:false -- label
its setRefusesFirstResponder:true -- no highlight
its setAlignment:(current application's NSTextAlignmentCenter) -- or whatever
its setFont:(current application's NSFont's fontWithName:"Noteworthy Bold" |size|:42)
its setTextColor:(current application's NSColor's redColor)
return it
end tell
end makeTextField
我想知道如何通过单击弹出式按钮来显示消息,这里有人可以指导我这样做吗? 例如,单击一个按钮会打开一个 Popover 视图,通知我我的 IP。
NSPopover 只是一个视图,您可以在另一个视图的某个位置显示它,因此您可以从按钮的操作处理程序中显示它。
举个例子,创建任何你想放在弹出框里的东西,比如 NSTextField,然后创建一个大的 NSViewController足以容纳它。接下来,创建 NSPopover,将其 contentViewController
设置为视图控制器,并通过其 showRelativeToRect:ofView:preferredEdge:
在所需位置显示它:
use framework "Cocoa"
property theWindow : missing value -- the main window
on run -- run on main thread if using the Script Editor
if current application's NSThread's isMainThread() as boolean then
doStuff()
else
my performSelectorOnMainThread:"doStuff" withObject:(missing value) waitUntilDone:true
end if
end run
to doStuff() -- make a window to put the example stuff in
set theWindow to current application's NSWindow's alloc's initWithContentRect:{{200, 400}, {200, 100}} styleMask:7 backing:(current application's NSBackingStoreBuffered) defer:true
theWindow's contentView's addSubview:(my makeButtonWithTitle:"Button" atLocation:{60, 40})
theWindow's makeKeyAndOrderFront:me
end doStuff
to makeButtonWithTitle:title atLocation:location -- make a button at {x, y}
tell (current application's NSButton's buttonWithTitle:title target:me action:"buttonAction:") -- 10.12 Sierra and later
its setFrameOrigin:location
its sizeToFit() -- fit to the title
its setRefusesFirstResponder:true -- no highlight
return it
end tell
end makeButtonWithTitle:atLocation:
on buttonAction:sender -- do something when the button is clicked
set title to sender's title
set testString to "This is some popover text
located at " & quoted form of (title as text) & "." -- whatever
set {{x, y}, {width, height}} to (sender's frame()) as list -- for adjusting the location of the pointer
set x to x + (width div 2) -- center horizontally
set y to y + (height div 2) -- center vertically
my showPopoverWithMessage:testString atPoint:{x, y} inView:(sender's |window|'s contentView()) -- use the window the button is in for the view
end buttonAction:
to showPopoverWithMessage:theMessage atPoint:thePoint inView:theView -- build and show the popover
set textField to makeTextField() -- make a text field
textField's setStringValue:theMessage -- put something in it
textField's sizeToFit() -- resize the text field to fit the string
set {width, height} to second item of (textField's frame as list) -- to size the view to the text field
set popoverView to current application's NSView's alloc's initWithFrame:{{0, 0}, {width + 15, height + 15}} -- make a view to hold everything
popoverView's addSubview:textField -- put the text field in it
set popoverViewController to current application's NSViewController's alloc's init -- make a controller for the popover view
popoverViewController's setView:popoverView -- set the view to it
tell current application's NSPopover's alloc's init() -- make the popover
its setContentViewController:popoverViewController
its setBehavior:(current application's NSPopoverBehaviorTransient)
its showRelativeToRect:{thePoint, {1, 1}} ofView:theView preferredEdge:(current application's NSMaxYEdge) -- and show it
end tell
end showPopoverWithMessage:atPoint:inView:
to makeTextField() -- make a text field for the popover
tell (current application's NSTextField's alloc's initWithFrame:{{10, 10}, {100, 100}})
its setBordered:false
its setDrawsBackground:false -- label
its setRefusesFirstResponder:true -- no highlight
its setAlignment:(current application's NSTextAlignmentCenter) -- or whatever
its setFont:(current application's NSFont's fontWithName:"Noteworthy Bold" |size|:42)
its setTextColor:(current application's NSColor's redColor)
return it
end tell
end makeTextField