Ok, I am trying to make a script where you type something and a new finder window appears with what you typed in it. Similar to spotlight search, but in a script.
set theFind to text returned of (display dialog "What do you want to find?" default answer "" buttons {"Cancel", "Ok"} default button 2)
tell application "Finder"
reveal theFind
end tell
There’s an AppKit method that does exactly what (I think) you’re asking for: -[NSWorkspace showSearchResultsForQueryString:], which means you can use it using AppleScriptObjC. So, in AppleScript Editor, File > New from Template > Cocoa-AppleScript Applet, then:
property NSWorkspace : class "NSWorkspace"
NSWorkspace's sharedWorkspace()'s showSearchResultsForQueryString_(theFind)
Alternatively, you could skip AppleScriptObjC and use the hidden Finder command that that method uses:
tell application "Finder" to «event aevtspot» theFind
Related
I'm trying to automatically change the directory of the frontmost "file open dialog" or NSOpenPanel dialog with AppleScript, whether that window is part of any application. The idea is that I hit a hotkey, and it will control that dialog to switch to a particular folder.
I can't seem to find out how to find the attributes of a window that would filter it for a "file open dialog". Using the Accessibility Inspector I can find that the "class" is NSOpenPanel. How can I get the class of a window using Applescript?
If you run the following AppleScript, you can see the properties of the foremost window:
tell application "anApp" to activate
delay 1
tell application "System Events"
tell process "anApp"
properties of window 1
end tell
end tell
The app has to be active to see the properties of the windows; You will not get consistent results if the app is in the background.
The NOOpenPanel ought to be recognizable by testing for some combination of the following properties:
role description:"dialog"
title:"Open"
subrole:"AXDialog"
name:"Open"
description:"dialog"
Personally, I'd probably rely on name and role description, which should be the same anytime an app throughs up a standard 'Open' dialog. 'Save' dialogs will be the same, except that title and name will be 'save' rather than 'open'.
If you have an app that presents a open or save sheet (a sub window attached to the titlebar), not a separate dialog, then you'll shift things a little. The AppleScript to get the properties looks like this:
tell application "anApp" to activate
delay 1
tell application "System Events"
tell process "anApp"
tell window 1
properties of sheet 1
end tell
end tell
end tell
and the relevant testable properties are as follows:
accessibility description:"save"
role description:"sheet"
role:"AXSheet"
description:"save"
You'll probably have to add logic to test whether the front window has a sheet, which should distinguish between dialogs and sheets.
Some apps use non-standard open/save dialogs, and you'll have to account for them on a case-by-case basis. There's no magic bullet for that.
The problem in using the standard procedure (via System Preferences… > Keyboard) is that it needs the exact command name…
Many thanks in advance for any answer.
--
kenNash
I am not aware of any wildcard which could be used there, probably there is none.
But there is a silly workaround:
EDIT:
Also, this can be easily done in the Automator.
Create new Automator document like this:
Here is the code:
on run {input, parameters}
tell application "System Events" to tell process "Finder"
tell menu 1 of menu bar item 3 of menu bar 1
click (menu item 1 where name starts with "Rename")
end tell
end tell
return input
end run
It has to be saved to the ~/Library/Services/ directory.
Then set your keyboard shortcut for service:
You will also need to give accessibility permissions to finder:
i would like to know if it's possible to change the look of Dialog Window in Applescript, for example change the icon (note, stop, caution) by a custom picture or icon instead.
And if it is possible to change the typography (alignment, font, bold ... etc.)
If yes, could you help me with an example for my code or provided a link to a good tutorial please!
set folderName to text returned of (display dialog "Please enter new folder name:" default answer "Folder_Name")
set loc to choose folder "Choose Parent Folder Location"
try
tell application "Finder"
set newFolder to make new folder at loc with properties {name:folderName}
end tell
display dialog "Successfully! Want to reveal the new folder?" with icon note buttons {"Cancel", "Go to my new folder"} default button "Go to my new folder" cancel button "Cancel"
if button returned of the result = "Go to my new folder" then
tell application "Finder"
reveal newFolder
activate
end tell
end if
end try
You can't change the typography. However you can use:
display dialog "hi" with icon withIconFile
where withIconFile is an alias or file reference to a ‘.icns’ file
This is better for me !
display dialog "My custom icon " buttons {"Cancel", "Continue"} default button "Continue" with icon file "Path:to:my.icon.icns"
You can also do:
display dialog "Hello" buttons {"Cancel", "Continue"} with icon {"/Users/" & (do shell script "whoami") & "/path/to/picture.jpg"}
The do shell script part just makes it work, even if you send it to your friends, although I would then recommend downloading the image from the internet with curl and placing it in a folder. Sorry if it is a bit confusing
I've been playing around with various UNIX commands and came across this one to display a dialog:
osascript -e 'tell app "System Events" to display dialog "Hello World"'
I'd like to change the position of the dialog. For most commands, I can just use man command to figure out how to do something for that command. However, man osascript doesn't tell me how I can change the position of the dialog box. How can I modify the command to put the dialog in a different place?
First, to get help with applescript just open the AppleScript Editor application and look under the help menu. The applescript language guide is in there and other tools. Also under the file menu is "Open Dictionary" which will show you the specific commands for applications.
To see the information about display dialog, open the dictionary of StandardAdditions and use the search field to find the command.
To answer your question, no, you cannot specify the location of a "display dialog" window. There are no parameters in that command for positioning the window, as you'll see in the dictionary. In addition, no other commands will help you either because you can't issue other commands while the dialog window is open because the code pauses while the window is open waiting for a response from the dialog window (like when you press the OK button).
If you need to set the position of a window to display information to a user then you'll need to use some other dialog tool other than "display dialog". You could create your own window in cocoa or google for some alternatives like cocoaDialog, SKProgressBar, and Notification Center.
There is a round-about way to go about this, which may be useful in some scenarios. Try the following:
on displayMessage(msg)
tell application "Finder"
activate
set c to (count windows)
ignoring application responses
display dialog msg with icon note
end ignoring
end tell
tell application "System Events"
tell application process "Finder"
repeat until ((count windows) > c)
delay 0.2
end repeat
set position of window 1 to {0, 22}
end tell
end tell
end displayMessage
displayMessage("I'm over here!")
Credit for this little script goes to a post here.
In implimenting this myself, I found it was limited to whether or not the application that is being called (Finder, in the example) supports the count window command (or whether it has API support at all).
I realise I've dragged up a question from 2013. I am posting this answer here in case it is of use to the OP or, more likely, someone else with a similar question.
I wrote an Service using Apple's Automator. The first action is an "Ask for Text" action. However, when I trigger the shortcut to initiate the service, the modal asking for text pops up, but it does not have focus. I have to use the mouse to click on it.
Is there any way to have automator open this window with the focus on the text input?
Add some applescript to the top of your workflow with the following code. Just replace myapp with your app name
tell application "myapp"
activate
end tell
This is a sort-of, works-for-me, partial answer; instead of using the Ask for text action, I just used an applescript within my automator app (instead of the Ask..action, I used the "Run applescript" action). it looks like this:
on run {input, parameters}
set x to the text returned of (display dialog "Enter a version number" default answer "1134" buttons {"OK"} default button 1)
return x
end run
My lil' automator application was to help me enter version numbers for tickets; using the mouse makes my hand hurt. The applescript dialogs seem to have focus properly, so enjoy! Let us know how you do!