How to update/reveal in Finder using Applescript - applescript

Here is what I would like to do:
IF file is already present in a finder window
tell finder to update...
ELSE
tell finder to reveal...
Is there a way to determine if the file in question is already displayed in a current Finder window? I do not want to end up creating a duplicate.

The reveal command should already work that way. activate focuses Finder if another application is frontmost:
tell application "Finder"
reveal POSIX file "/usr/share/doc/bash/bash.html"
activate
end tell

Related

Show/hide Finder, and create new window if none exist via AppleScript

I'd like to create an AppleScript to trigger via key command in Keyboard Maestro that allows me to toggle showing or hiding the Finder window(s) directly. And if when toggling to show the Finder, if there are no existing windows, create one and open it up to my home directory.
The following AppleScript works. However, there seems to be a race condition between activating the finder and detecting if there are any open windows with if not (window 1 exists), hence the delay 0.5.
The problem (which I think is a race condition in detecting the presence of an existing Finder window) results in this script frequently creating new Finder windows when one already existed. The if not (window 1 exists) doesn't always get it right.
Any thoughts, tweaks, or affirmations that this is just the way it is would be appreciated!
tell application "System Events"
set activeApp to name of application processes whose frontmost is true
if ((activeApp as string) is equal to "Finder") then
set visible of process "Finder" to false
else
tell application "Finder"
activate
delay 0.5
if not (window 1 exists) then
make new Finder window
set thePath to POSIX file "/Users/jon"
set the target of the front Finder window to folder thePath
end if
end tell
end if
end tell
Please try this simpler syntax, it uses only Finder terminology
tell application "Finder"
if frontmost then
set visible of process "Finder" to false
else
if (count windows) is 0 then reveal home
activate
end if
end tell
Edit:
To run a Keyboard Maestro macro, open Keyboard Maestro Editor, select the macro and then select Copy as > Copy UUID from the Edit menu.
Then in AppleScript write
tell application "Keyboard Maestro Engine" to do script "<Script-UUID>"
replacing <Script-UUID> with the copied real UUID
Ultimately I needed to activate Finder before running the count windows command or I'd get inconsistent window counts. Sometimes it'd be 0 even when there was a window open already. This code is working well for me so far.
tell application "Finder"
if frontmost then
set visible of process "Finder" to false
else
activate
if (count windows) is 0 then
open home
tell application "Keyboard Maestro Engine" to do script "<Script-UUID>"
end if
end if
end tell

Automator not working inside xcode

I am running an automator with an applescript, and while it works inside automator, it fails when run from xcode.
Here's my code:
tell application "Xcode"
set targetProjectPath to path of active workspace document
set targetProjectPath to POSIX file (targetProjectPath & "/..") as string
set targetProjectPath to POSIX path of targetProjectPath
tell application "System Events"
tell process "Xcode"
click menu item "Save" of menu 0 of menu bar item "File" of menu bar 0
end tell
end tell
return {targetProjectPath}
end tell
I am trying to trigger save and return me the path of the active workspace to do some work later.
I just started on it few days back, so pardon me if it's something really stupid.
It was really simple. All I needed was to give xcode access to be able to modify accessibility settings. The code is working perfectly fine.

Which message could applescript tell the application?

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

Creating an automator service to create a new document in the current directory

so I'm trying to create a service that will be located in the contextual menu of the Finder and that would allow to create a new document in the current directory.
I've been doing that using Automator:
Sorry everything's in French ^^
Anyway here's the AppleScript that I'm using to retrieve the current working directory:
on run {input, parameters}
tell application "Finder"
set pwdAlias to insertion location as alias
if not (exists folder pwdAlias) then
set pwdAlias to (container of pwdAlias) as alias
end if
end tell
set pwd to POSIX path of pwdAlias
return pwd
end run
Then I'm setting this value to a variable, then creating a new text document using the variable as the path for the document and finally I'm using the command Reveal in Finder to show the created document.
Everything's is working fine except that the script seems to always be late!
What I mean is that when I open a new Finder window and select my service, it is systematically creating the document on the previous window as shown below:
But then if I try a second time, the document is being created properly at the expected location:
And this is very systematic it happens every time!!
Sorry if I'm not very clear, it is not so easy to explain!
Well otherwise, I'm running Mountain Lion and here's the Automator project attached: create_new_document
To add the service just unzip and put the file under ~/Library/Services/
Hope to get some answers but I fear that this is just an Automator bug!
Try this
Depending on what you want to be clicking.
Set the Services selected to: 'folders'
or files or folders. in 'Finder.app'
Get first Finder Window path Action
You can download the Get first Finder Window path Action from my blog post here
The download is at the bottom of the post.
The Action gets the posix path of the frontmost finder window.
Since you are clicking on a folder in a window. that window will be the one returned.
Set Value of Variable
Get Specified Text
The next action 'New Text File' needs some input. If it does not get any, no file will be created. You can leave the text field blank. Just having the action in place works.
New Text File
Drag the Variable 'path' or what ever you named it on to the Where: drop down menu.
you can click the double blue lines at the bottom of the Automator window to toggle the workflow Variable List
Save your service. And try it.
(It may take a short while to show up in the contextual Menu.)
It's an open bug in 10.7 and 10.8
Use this Workaround
on run {input, parameters}
activate application "System Events"
activate application "Finder"
tell application "Finder"
set pwdAlias to insertion location as alias
set pwdAlias to (container of pwdAlias) as alias
end tell
return POSIX path of pwdAlias
end run

How to set the file in rename mode in apple script?

I know this question is a little bit crazy, but I do want to know the solution. Is there any way to run an apple script to set any given file or folder in rename mode so that the user can type in the new name? I know I can do the rename easily in script but I am just wondering whether it is doable or not.
Thanks in advance!
Well, you can sort of hack it together with System Events
tell application "Finder"
activate
select file "test.png" of desktop
tell application "System Events" to keystroke return
end tell
It's kind of a hack, though, but I don't know if there's another way to do it
It is possible using UI scripting. First select whichever item you're interested in---presumably through AppleScript in practice, but by hand is fine for trying it out. Then:
tell application "Finder" to activate
tell application "System Events" to keystroke return

Resources