Open new Finder window using AppleScript - applescript

How can I open a new window of Finder in AppleScript? I am using an if else statement to open a window. I tried an Osascript command but it gave me a syntax error. The code is below:
if the button returned of the result is "Sleep" then
tell app "Finder" to go to sleep
if the button returned of the result is "Open Finder" then
osascript -e 'tell application "Finder" to open new window'
else
display dialog current time
end if

Try:
tell application "Finder" to make new Finder window

My Alfred Workflow script:
on alfred_script(q)
tell application "Finder"
make new Finder window
end tell
end alfred_script

Related

Execute shortcut or menu action in particular application window

Is there a script option to actualy go through all XCode opened projects (application windows) and execute menu action Product->Stop or executing shortcut "CMD + ."?
To run Apple Script in Xcode at the beginning of each run You can create shell script like so:
#!/bin/bash
osascript -e 'tell application "Xcode"
activate
set activeWindow to window 1
set windowName to name of activeWindow
repeat with aWindow in windows
tell application "System Events" to keystroke "`" using command down
if name of aWindow is not equal to windowName then
if name of aWindow is not equal to "" then
tell application "System Events" to keystroke "." using command down
end if
end if
end repeat
end tell'
Then in Xcode go to Preferences->Behaviours->Starts->Run and choose Your script.

How do I open a folder with AppleScript in the current finder window?

I know tell application "Finder" to open POSIX file "/folder/path/" will open a new finder window, how can I open a folder in the current finder window?
I found the answer myself:
tell application "Finder"
set the target of the front Finder window to (POSIX file "/folder/path/")
end tell

AppleScript cannot remove files from /Library

I write an AppleScript:
to check if two specific dictionaries (File1 and File2) files exit in /Library/Dictionaries/
if either of the two files exits, then completely delete that file and the other
The script works fine in AppleScript Editor. I then save the script as an app, test it, and the app also works just fine. I mean both the script in AppleScript Editor and the saved app detect and remove both File1 and File2 from /Library/Dictionaries/.
However in PackageMaker Post Action, when called, the app removes neither File1 nor File2 although it detects them and even shows the dialog message (see code line below).
Here is the code:
tell application "System Events"
if exists (application process "Dictionary") then
tell application "Dictionary" to quit
end if
end tell
try
set theOtherFile1 to POSIX file "/Library/Dictionaries/File1.dictionary"
set theOtherFile2 to POSIX file "/Library/Dictionaries/File2.dictionary"
tell application "Finder"
if exists file theOtherFile1 then
display dialog "File1/File2 exits. Do you want to remove it?" with title "Note" buttons {"No", "Yes"} default button "Yes"
if the button returned of the result is "No" then
delay 2
else
do shell script "rm -R /Library/Dictionaries/File1.dictionary" with administrator privileges
do shell script "rm -R /Library/Dictionaries/File2.dictionary" with administrator privileges
end if
end if
end tell
end try
delay 5
tell application "Dictionary" to activate
Try this:
tell application "System Events"
if exists (application process "Dictionary") then
tell application "Dictionary" to quit
end if
end tell
try
set theOtherFile1 to POSIX file "/Library/Dictionaries/File1.dictionary"
set theOtherFile2 to POSIX file "/Library/Dictionaries/File2.dictionary"
tell application "Terminal" to activate
tell application "System Events"
keystroke ("sudo chmod 777 " & theOtherFile1) as string
display dialog "Click the button after you've typed the root password into Terminal." buttons {"OK"}
keystroke return
keystroke ("chmod 777 " & theOtherFile2) as string
display dialog "Click the button after you've typed the root password into Terminal." buttons {"OK"}
end tell
do shell script "rm -rf /Library/Dictionaries/File1.dictionary"
do shell script "rm -rf /Library/Dictionaries/File2.dictionary"
This should do the trick.
I'm using a PC at the moment, otherwise I'd check first, but I think this will run correctly.

AppleScript Focus / Rename File (and clicking anywhere)

on run {input}
set filepath to POSIX path of input
do shell script "touch " & quoted form of filepath & "untitled"
return input
end run
Is what I have so far, and it works, but is there a way to then focus on the file then trigger a rename? I dont want the rename to be automatic, just trigger the event (like pressing "return" while you have a file selected). And I dont want to use any sort of modal...
Quick Side question: is there a way to set this so that i dont have to select a folder or file directly, but can do it by, lets say, clicking in a white space in a folder as long as it's in Finder? Right now I have my "Service receives selected" to "files or folders" in Finder.app.
== UPDATED CODE ==
on run {input}
set filepath to POSIX path of input
do shell script "touch " & quoted form of filepath & "untitled"
tell application "Finder"
activate
set target of Finder window 1 to POSIX file "/Users/oscargodson/Documents/designs/untitled"
end tell
tell application "System Events"
tell process "Finder"
keystroke return
end tell
end tell
return input
end run
If i hardcode the path it works! But how do I get it as a var that works?
Here's one way. I think a modal window where you ask for the name would be better but you can try this. Notice you do not use "POSIX path" in this code. Applescript doesn't use POSIX paths. Also {input}, as indicated by the brackets around it, is a list of items. Therefore you act on the items of the list, and in this case we act on the first item.
set filepath to item 1 of input
tell application "Finder"
activate
reveal filepath
end tell
tell application "System Events"
tell process "Finder"
keystroke return
end tell
end tell
EDIT: With your updated code, here's a working script...
on run {input}
if (class of input) is not list then set input to {input}
set theFolder to (item 1 of input) as text
try
alias theFolder
tell application "Finder"
if (class of item theFolder) is not folder then error "input is not a folder."
activate
set theFile to make new file at folder theFolder with properties {name:"untitled"}
reveal theFile
end tell
delay 0.2
tell application "System Events"
tell process "Finder"
keystroke return
end tell
end tell
on error theError number errorNumber
tell me
activate
display dialog "There was an error: " & (errorNumber as text) & return & return & theError buttons {"OK"} default button 1 with icon stop
return
end tell
end try
return input
end run
tell application "Finder"
activate
reopen -- in case there are no open windows
set target of Finder window 1 to POSIX file "/Applications/Safari.app"
end tell
reveal and select always open a new window, set target and set selection don't.
I don't know why, but when set selection it used in column view, you can only select items in the entire contents of the target of the front window. The same thing doesn't happen in other views, so it seems like a bug.
Fix for the code in the edited question:
on go(input)
set p to POSIX path of (input as text)
set p2 to p & "untitled"
do shell script "touch " & p2
tell application "Finder"
reopen
activate
set target of Finder window 1 to POSIX file p2
end tell
delay 0.3 -- time to release modifier keys
tell application "System Events" to keystroke return
end go
tell application "Finder"
set fold to folder (path to documents folder)
end tell
go(fold)
(That on go and the last lines are just for testing.)
I've created an AppleScript based on the #regulus6633's one, but with some improvements.
Note: This answer was originally posted as an AskDifferent answer. I'm copy/pasting here for convenience.
The idea is to create an Automator workflow and assigning a shortcut to it using the following steps:
Open Automator and create a Service;
Set the input to no input, and the application to Finder.app;
Drag and Drop the Run AppleScript workflow element onto the grey space;
Put the contents of this AppleScript in the textbox;
Save the workflow with a reasonable name (like New File);
Go to Settings -> Keyboard -> Shortcuts -> Services and assign a shortcut to it.
Now, let's show the AppleScript:
set file_name to "untitled"
set file_ext to ".txt"
set is_desktop to false
-- get folder path and if we are in desktop (no folder opened)
try
tell application "Finder"
set this_folder to (folder of the front Finder window) as alias
end tell
on error
-- no open folder windows
set this_folder to path to desktop folder as alias
set is_desktop to true
end try
-- get the new file name (do not override an already existing file)
tell application "System Events"
set file_list to get the name of every disk item of this_folder
end tell
set new_file to file_name & file_ext
set x to 1
repeat
if new_file is in file_list then
set new_file to file_name & " " & x & file_ext
set x to x + 1
else
exit repeat
end if
end repeat
-- create and select the new file
tell application "Finder"
activate
set the_file to make new file at folder this_folder with properties {name:new_file}
if is_desktop is false then
reveal the_file
else
select window of desktop
set selection to the_file
delay 0.1
end if
end tell
-- press enter (rename)
tell application "System Events"
tell process "Finder"
keystroke return
end tell
end tell
For convenience, I'm putting this AppleScript in this GitHub Gist.

Applescript to make new folder

I Want to make a new Folder command in apple script
Why dosent this script work?
tell application "Finder"
activate
end tell
tell application "System Events"
tell process "Finder"
tell menu bar 1
tell menu bar item "File"
tell menu "File"
click menu item "New folder"
end tell
end tell
end tell
end tell
end tell
You can do it more directly with AppleScript:
tell application "Finder"
set p to path to desktop -- Or whatever path you want
make new folder at p with properties {name:"New Folder"}
end tell
I don't know if running bash commands within AppleScript is cheating, but you can also do:
do shell script "mkdir ~'/Desktop/New Folder'"
Which is useful when you need to create sub folders on-the-fly when they don't exist yet:
do shell script "mkdir -p ~'/Desktop/New Folder/Bleep/Bloop'"
tell application "Finder"
activate
end tell
tell application "System Events"
tell process "Finder"
tell menu bar 1
tell menu bar item "File"
tell menu "File"
click menu item "new folder"
end tell
end tell
end tell
end tell
end tell
--you capitalize the N in new folder the new folder button is not capped.
NOTE: This can fail for two reasons;
(1) '~' trapped in singlequote won't parse.
(2) space in '/New Folder/' will break the path.
do shell script "mkdir -p '~/Desktop/New Folder/Bleep/Bloop'"
SOLVED:
do shell script "mkdir -p ~/Desktop/" & quoted form of "New Folder/Bleep/Bloop"
You can directly with an applescript script by simulating keystroke on ("N" and command and shift) this will create a new folder on the desktop or in the open Finder window.
Below the script, you can test it in the script editor
tell application "System Events" to tell process "Finder"
set frontmost to true
keystroke "N" using {command down, shift down}
end tell
Your script works if you add under "tell process" Finder "
"set frontmost to true"
Which give
tell application "System Events"
tell process "Finder"
set frontmost to true
tell menu bar 1
tell menu bar item "File"
tell menu "File"
click menu item "New folder"
end tell
end tell
end tell
end tell
end tell
tell application "Finder"
set thepath to alias "Macintosh HD:Users:JasonMagnuson:Documents:" as text
make new folder at thepath with properties {name:"nov_archive"}
end tell

Resources