When I want to do something with Finder, I use
tell application "finder"
do something
end tell
Is there a function list that finder can do?
Or for other application?
In AppleScript the terminology of the applications is listed in dictionaries
In Script Editor select menu item File > Open Dictionary… or press Shift Cmd O.
Then choose Finder or another application
Related
How can choose the file from the dialog panel by using apple script?
When I write the script to activate the application, then it need to choose the file to open from the pop up dialog panel.
So, how to write of this script?
If I understood you right, you mean something like this:
set theFile to choose file
tell application "" --enter the application you want to activate and open your file here
activate
open theFile
end tell
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 am very new to both automator and applescript. I have created an application that uses automator to print file(s) that are dragged and dropped onto the program icon. it is very simple. In automator, I just run
Print Finder Items
and the program works. Now I need to do more complicated things and I need to figure out how to convert that one command into applescript. I've tried the advice given in multiple stack overflow articles including: Automator to Applescript for Editing Code but it does not help me. How do I convert this to applescript?
tell application "Finder"
set theItems to selection
print theItems
end tell
You need to learn how to find the commands yourself so you can explore what they do. Open AppleScript Editor. Under the File menu choose "open dictionary" and select the Finder. This dictionary tells you everything you can do with applescripting the Finder. Each application has its own dictionary so you need to start looking at these.
That script works on any files you select in the Finder. Good luck.
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.