Apple Script: select files on desktop - applescript

I want to select some files (for example, "test.txt") on the desktop using Apple Script.
I do that:
tell application "System Events" to key code 103
(simulation press key "F11") to show desktop.
And after that I would like to select my file "test.txt" and see this selection on the desktop. This code:
tell application "Finder" to select file "test.txt" of folder "Desktop" of home
select my file in "Finder". This is not what I want to.
I want to see the selection of my files on the desktop (not in the window of Finder, that show folder 'Dektop')

Change the selection property instead of using the select command:
tell application "Finder"
set selection to item 1 of desktop
end tell
This would also keep existing selections:
tell application "Finder"
activate
select window of desktop
if selection is {} then set selection to item 1 of desktop
end tell

Related

Unable to find the application support folder for a specified application inside an Apple Script

I have an apple script that asks the user to select an application as a prompt. I would like to know how it could be possible to find the directory of the Application Support folder for this application per example, Spotify is
~/Library/Application Support/Spotify/
On my mac, but like is there a way inside apple script to find this directory for the specific app. It would really be appreciated, thanks!
Because I would like to then delete this folder using Apple Script
This should accomplish what you're looking to achieve. Be careful though because using System Events to delete... deletes the item permanently instead of sending it to the Trash
property folderNames : missing value
tell application "System Events"
set appSupportFolder to application support folder
set folderNames to name of folders of appSupportFolder
set theChoice to my chooseFolderToDelete()
delete folder theChoice of appSupportFolder
end tell
to chooseFolderToDelete()
activate
set theChoice to (choose from list folderNames with prompt ¬
"Select an Application's Support folder to delete" OK button name ¬
"Delete Folder" cancel button name "Cancel") as text
end chooseFolderToDelete

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

Applescript click 'share dropbox link' using contextual menu

I am trying to access the 'share dropbox link' item the contextual menu of a specific file in a specific dropbox folder using AXShowMenu.
I have found the following script. Via Applescript right click a file
tell application "System Events"
tell process "Finder"
set target_index to 1
set target to image target_index of group 1 of scroll area 1
tell target to perform action "AXShowMenu"
end tell
end tell
Which seem to do the trick for desktop items,
but how can I use this to target specific files in folders then click the 'share dropbox link' in the contextual menu?
A little kludgey, but it does work. This requires you have the dropbox folder open and frontmost in Finder.
tell application "Finder"
activate --brings Finder to front
select item 1 of window 1 --selects the first item in the list ; not sure how you want to select/determine the item
end tell
delay 0.5 --this is to insure you don't get unwanted typing in script editor (makes sure Finder activates before kludgey typing gets done)
tell application "System Events"
tell application process "Finder"
set _selection to value of attribute "AXFocusedUIElement" --focused element (selected element)
tell _selection to perform action "AXShowMenu" --contextual menu
end tell
keystroke "share dropbox link" --type to select contextual menu item
keystroke return --select it by hitting return
end tell
[EDIT] Here's a trick to make sure your dropbox folder is open and frontmost in the Finder:
tell application "Finder"
activate --brings Finder to front
tell me to set dbFolder to POSIX file ((do shell script "cd ~/Dropbox/;pwd") & "/")
open dbFolder
select item 1 of window 1 --selects the first item in the list ; not sure how you want to select/determine the item
end tell
tell application "System Events"
tell application process "Finder"
set _selection to value of attribute "AXFocusedUIElement" --focused element (selected element)
tell _selection to perform action "AXShowMenu" --contextual menu
end tell
keystroke "share dropbox link" --type to select contextual menu item
keystroke return --select it by hitting return
end tell
There may be another way of selecting the contextual menu, but this is what I came up with so far late at night after a glass of wine.

Applescript - Copy Image, Paste as Icon

For organizational purposes, via a hotkey, I'd like to be able to quickly copy a selected image file (as is in Finder), then paste it as the icon for a selected folder, without having to go into 'Get Info' for the folder.
Is this possible?
You probably need two scripts: one to copy image (not a file but image itself), other to paste image as icon.
Copy image:
tell application "Finder"
open selection using (path to application "Preview") -- open with Preview
end tell
tell application "Preview"
tell application "System Events"
keystroke "a" using command down -- select all
keystroke "c" using command down -- copy
keystroke "w" using command down -- close window
end tell
end tell
Paste image as icon:
tell application "Finder"
activate
tell application "System Events"
keystroke "i" using command down -- open info
-- set focus on icon image
tell process "Finder"
set img to (image 1 of scroll area 1 of front window)
set focused of img to true
end tell
keystroke "v" using command down -- insert image
keystroke "w" using command down -- close window
end tell
end tell
Than you can bind these two scripts to some hotkeys. I use FastScripts for that purpose.
Note 1: you may need to enable access for assistive devices under the SystemPreferences -> Accessibility
Note 2: if some parts of the scripts are not work (for example Preview opened, but image not selected, etc.) you should try to play with delays.

Applescript to open specific Terminal Style Windows

Ok, so the thing is. I want to have different style terminal windows for each job. Each one will have a different job, i.e. , one will connect through ssh to a site, other window to other place, etc.
So I guess this could be done with some Applescripting?
The thing would be to have some applescripts that opens a different terminal window. And then add each applescript to a shortcut.
Any ideas?
Thanks :)
How about setting up a Window group in terminal ?
Open all Terminal windows you want --> Shell --> Show Inspector. Under Settings you can change the theme of each terminal window.
Window --> Save Windows as Group
In preferences set the startup option to display the group.
http://img18.imageshack.us/img18/9681/screenshot20111018at110.png
http://img542.imageshack.us/img542/9681/screenshot20111018at110.png
If you want to use Applescript to set a window's theme you first need to get the id's of all the themes you have using this applescript :
set a to {}
tell application "Terminal"
repeat with i from 1 to count settings set
set temp to {settings set i's name, settings set i's id}
set end of a to temp
end repeat
a
end tell
This will output an array of id # and the theme's name. Next to create a new window use the following :
tell application "Terminal"
set a to do script "" -- creates new window
set a's current settings to (settings set id <one of the id #>)
end tell
tell application "System Events" to tell process "Terminal" to click menu bar 1's menu bar item "Shell"'s menu 1's menu item "New Window"'s menu 1's menu item "Grass"
tell application "Terminal"
set win to do script
set win's current settings to settings set "Basic"
end tell

Resources