Apple script error Finder got an error: AppleEvent handler failed - applescript

This is the script i am trying to make
i am trying to make a script to see if Fido_Data exsists on my desktop with a if and else statement but for some reason it just gets the error Finder got an error: AppleEvent handler failed.
tell application "Finder"
if exists folder [homePath & "Desktop/Fido_Data"] then
set FidoFolderExists to "yes"
else
display dialog homePath & "Desktop/"
make new folder at [homePath & "Desktop/"] with properties {name:"Fido_Data"}
end if
end tell

Try:
tell application "Finder"
if exists folder ((path to desktop as text) & "Fido_Data") then
set FidoFolderExists to "yes"
else
display dialog (path to desktop as text)
make new folder at (path to desktop) with properties {name:"Fido_Data"}
end if
end tell

I got stumbled across intermittent cases of finder getting file existence wrong in 10.10.5. I suggest:
-- ------------------------------------------------------
(*
Philip Regan
https://stackoverflow.com/questions/3469389/applescript-testing-for-file-existence
*)
on fileExists(theFile) -- (String) as Boolean
tell application "System Events"
if exists file theFile then
return true
else
return false
end if
end tell
end fileExists

Related

After installing macOS-sierra apple script showing actual wallpaper in finder not working any longer

I used to use the following apple script to find out the actual wallpaper image on my monitor (dual mode) and show it in finder. Under El Capitan the script works fine. After I installed Mac OS Sierra the script shows the error message
"error "„Finder“ hat einen Fehler erhalten: Die Routine kann Objekte dieser Klasse nicht bearbeiten." number -10010"
English Translation:
"error "„Finder“ received an error: The routine cannot work with objects of this class.“ number - 10010". The object which is highlighted in the script is "reveal rotationImage"
I am not a apple script specialist. Unfortunately I couldn't find any help on the web. What could be the problem?
tell application "System Events"
set posix_path to (pictures folder of desktop 1)
set picPath to (POSIX file posix_path) as string
end tell
set thePictures to (do shell script "sqlite3 ~/Library/Application\\ Support/Dock/desktoppicture.db \"SELECT data.value FROM preferences INNER JOIN data on preferences.key=16 and preferences.picture_id=5 and preferences.data_id=data.ROWID\"")
set fullPath to picPath as string
set rotationImage to fullPath & thePictures
tell application "Finder"
reveal rotationImage
activate
end tell
tell application "Finder"
get selection
repeat with moose in result
if original item of moose exists then
reveal original item of moose
end if
end repeat
end tell
The script works on my machine even on Sierra, a failure reason could be that reveal expects a file reference rather than a literal string.
This is a slightly optimized version of your script:
tell application "System Events"
set posix_path to (pictures folder of desktop 1)
set picPath to (POSIX file posix_path) as string
end tell
set thePictures to (do shell script "sqlite3 ~/Library/Application\\ Support/Dock/desktoppicture.db \"SELECT data.value FROM preferences INNER JOIN data on preferences.key=16 and preferences.picture_id=5 and preferences.data_id=data.ROWID\"")
set fullPath to picPath as string
set rotationImage to fullPath & thePictures
tell application "Finder"
try
set aliasItem to item rotationImage
if class of aliasItem is alias file then
reveal original item of aliasItem
end if
end try
end tell

Applescript setting desktop picture to file in current directory

I'm attempting to set the current desktop picture to an image in the current directory of the script. For example, if my folder structure is:
~/Documents/Scripts/DesktopImageScript/image.jpg
With the script in the same directory, I want to be able to set the desktop image to the image.jpg without directly referring to the folder structure. The code I am currently using to fetch the current directory is:
tell application "System Events" to set app_directory to POSIX path of (container of (path to me))
The issue isn't in that code as I can run the following command with the expected and correct results:
do shell script "echo " & app_directory
I believe the issue is in the code I'm using to set the desktop image:
tell application "Finder"
set desktop picture to POSIX file (quoted form of POSIX path of (app_directory & "/image.jpg"))
end tell
The error I receive when I try to run the script is:
error "Finder got an error: AppleEvent handler failed." number -10000
Not really sure what could be causing the error or how to fix it. Any help is appreciated. The full script is below:
tell application "System Events" to set app_directory to POSIX path of (container of (path to me))
tell application "Finder"
set desktop picture to POSIX file (quoted form of POSIX path of (app_directory & "/image.jpg"))
end tell
Solved by using code from another answer on StackOverflow.
tell application "System Events"
set theDesktops to a reference to every desktop
repeat with x from 1 to (count theDesktops)
set picture of item x of the theDesktops to app_directory & "/image.jpg"
end repeat
end tell

Applescript path to me returning error

I have an applescript script (.scpt) with the following function:
on scrap_range(min, max)
repeat with loop from min to max
tell application "Safari"
-- Do stuff
end tell
tell application "Finder"
set outputFile to ((container of (path to me)))
end tell
display dialog (outputFile as text)
end repeat
end scrap_range
When I run it in Applescript Editor it fails with the following event log output.
tell current application
path to current application
Result:
error "File some object wasn’t found." number -43
And it highlights the "path to me" text in the code. Where am I going wrong? Many thanks.

Simple backup of folder on usb stick with AppleScript

I'm a new user of AppleScript and I try to settle an script to backup a folder from my mac to a folder on a usb stick.
I started to create this script but it doesn't work.
tell application "Finder"
duplicate folder "/Users/alex/Desktop/test/" to "/Volumes/myusb/test/" replacing yes
end tell
Thanks for you help.
This should work:
set SourceFolder to POSIX file "/Users/alex/Desktop/test/"
set TargetFolder to POSIX file "/Volumes/myusb/"
tell application "Finder"
if exists SourceFolder then
try
duplicate SourceFolder to TargetFolder replacing yes
on error the error_message number the error_number
display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
end try
end if
end tell
("POSIX file" is from the "StandardAdditions")

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.

Resources