I'm trying to create a simple script to delete files based on a custom label I've already assign.
I'm currently trying to limit the search for the script to a test folder, but ultimately I want the script to search in all the user folder and get all the files from several different locations. I may need authentication for the process.
But so far I have this
tell application "Finder" delete (every item of folder
"/users/ro/documents/Erase test" whose label is "test") end tell
and I get this error
error "Finder got an error: Can’t get folder
\"/users/ro/documents/Erase test\"." number -1728 from folder
"/users/ro/documents/Erase test"
As I said I don't really know much about scripts, so I don't know all the terms but I hope someone can point me in the right direction.
Saw this late.
Tested this on 10.6.8 and will jump on a Mavericks machine to test, but this should work:
set f to choose folder
tell application "Finder"
delete (every item of f whose label index is 1)
end tell
A few notes about your attempt:
1) AppleScript doesn't 'natively' understand POSIX paths (but coercion to/from is possible), so (as I have it) "choose folder" returns what is known as an alias (not to be confused with a string -- but again, coercions to/from strings/aliases are simple).
2) note that the label is recognized as "label index", which is an integer.
3) you could/should test by taking out "delete" in that line to return a list of those items.
[edit] yes, this is fine on Mavericks.
Related
Today is my first day of even knowing AppleScript exists so I apologize if this is a stupid question. I've searched and can't find the answer on how to simply move a file with AppleScript
All I need to do is move a file from ~/Downloads/blank.potx to ~/Library/Application Support/Office/User Templates/My Templates
This is what I have in AppleScript right now:
tell application "Finder"
move "~/Downloads/blank.potx" to "~/Library/Application Support/Microsoft/Office/User Templates/My Templates/blank.potx"
end tell
When I run this it gives me an error:
error "Finder got an error: AppleEvent handler failed." number -10000
Again, first day using AppleScript and I'm lost. Any help you can provide would be awesome.
If there's a better way to do this please let me know as well.
Thanks!
Search is your friend. You can easily find the syntax on stack overflow. Such as here:
tell application "Finder"
move POSIX file "/Users/xx/Documents/img.jpg" to POSIX file "/Users/xx/Documents/State" with replacing
end tell
If you're gonna use Posix file paths, you need to indicate so, and you need to name the string as a file.
Some Background: I've used Skim with BibDesk for a while now for reading and annotating scientific journal articles. Recently, I purchased an android tablet and would like to use it to read and annotate .pdfs as well. I use the reference library Eratosthenes with ezPDF Reader and sync all files through Dropbox. The issue I'm having is that Skim stores annotations as extended attribute files by default, which are not accessible to other devices through Dropbox. I've worked around this problem by saving the annotations as .fdf files and then linking the .fdf files to the citation entry in BibDesk. ezPDF reader can import .fdf files as annotations, and if the annotation file is linked to the BibDesk entry, both the .pdf and .fdf can be easily downloaded to the tablet without needing to sync the entire Dropbox folder full of hundreds of references.
I'd like to write an applescript that does this automatically, but am very new to applescript and am having a hard time getting started. I've written the following script to be executed when "command-s" is pressed while in skim:
tell application "Skim"
set docPath to path of front document
set notesPath to text 1 thru -5 of docPath & ".fdf"
save front document
save front document in notesPath as "Notes as FDF"
end tell
This essentially saves the current document while simultaneously exporting an .fdf file. Within the same script, I would like to link the .fdf file to the appropriate citation entry in BibDesk. This would involve:
determining the citation entry name associated with the .pdf (maybe search through entries to locate one linked with the front document)
check to see if the .fdf file is already linked to it
if not, attach .fdf file
I haven't been able to find someone who's done something similar, and really can't get past the first step. I tried writing something basic (assume citation entry is highlighted, assume .fdf file is not linked), which produces no results:
tell application "BibDesk"
set thePub to selection
tell thePub
set theFieldName to "Local-URL-2"
set value of field theFieldName to fdfPath
end tell
end tell
Is anyone familiar with Bibdesk applescripts able to help me with the second part of this code?
Thank you very much in advance.
I'm not too familiar with scripting BibDesk, but I've been poking around a little. Heavily commented to help guide you:
set theFieldName to "Local-URL-2"--generally better not to put
--something like this in a tell block if it isn't necessary
tell application "BibDesk"
--as you have it, you are simply putting the selection class
--into a variable. here we reference the specific selection
--object of the document object:
set thePubSel to selection of document 1
--and, since this returns a *list* of pubs, I'm grabbing just the first item
--(but a loop iterating through every publication in the selection perhaps better)
set thePub to item 1 of thePubSel
tell thePub
set value of field theFieldName of it to fdfPath
end tell
end tell
The following code answers my initial question satisfactorily. The first section reiterates the saving and exporting commands. The second section locates the citation entry containing the linked .pdf file (front document in Skim). The third section attaches (links) the .fdf file to the citation entry (thanks to CRGreen for some help here).
--save and export .fdf file
tell application "Skim"
set docPath to path of front document
set fdfPath to text 1 thru -5 of docPath & ".fdf"
save front document
save front document in fdfPath as "Notes as FDF"
end tell
--search for relevant citation entry
tell document 1 of application "BibDesk"
--sort all publications in library by Cite Key
set thePubs to (sort (get publications) by "Cite Key")
-check each publication individually (surely this is not the most efficient way to do this)
repeat with aPub in thePubs
--check to see if the .pdf is in the citation entry
tell aPub
if linked files contains (POSIX file docPath) then
set thePub to aPub
--once the citation is found, exit loop
exit repeat
end if
end tell
end repeat
--link the .fdf file to the citation entry (if it isn't already)
tell thePub
--if the fdf file exists in the linked file, do nothing
if linked files does not contain (POSIX file fdfPath) then
add (POSIX file fdfPath) to end of linked files
end if
end tell
end tell
I'm assuming that there is a better way to search for the citation entry with the associated .pdf file (maybe using the applescript search command?). This solution works for me, but if you know of a more elegant way to solve this problem, feel free to mention it.
To map the script to a keyboard shortcut ("command-s" is convenient), see here (the menu title is the name of your script). The script needs to first be saved to the ~/Library/Application Support/Skim/Scripts directory.
I'm just learning applescript, and I realize that this may have been a very trivial exercise for most, but perhaps it will help out another beginner.
So, i'm working on a script that copies the home folder to a mounted shared drive folder. But i'm getting the following error:
"Handler can't handle objects of this class number -10010"
This is the code I came up with following the example of other code i've seen on here. I'm guessing that it is the way i'm telling finder to duplicate.
set vserver to ("/Volumes/sharedfolder")
set source to ("/Users/user")
tell application "Finder"
duplicate source to vserver
end tell
How else can I write this?
I've also tried running a boolean test to see if Finder saw the shared folder or my home folder and it retured false. (but only one false when it should have returned two)
tell application "Finder"
setaBoolean1 to get (exists vserver)
setaBoolean1 to get (exists source)
end tell
set vserver to ("/Volumes/sharedfolder")
The line above sets the variable vserver to a string object consisting of "/Volumes/sharedfolder". Likewise, the set source to "/Users/user" line sets source to a string object containing "/Users/user". Note that strings are not what the Finder is expecting when you're telling it to duplicate items.
The tell app Finder line is basically trying to tell the Finder to duplicate one string into another string, which it doesn't know how to do (hence the Handler can't handle objects of this class message).
What you need to do is to, instead of creating strings, create some sort of file system reference to those folders, so that the Finder knows how to deal with them.
There are numerous ways to do this, but the method I found that works (which uses the same POSIX style path format) is the following:
set vserver to POSIX file "/Volumes/sharedfolder"
set source to POSIX file "/Users/user"
tell application "Finder"
duplicate source to vserver
end tell
I have tried all day now to do the following:
I have a folder with 99 subfolders. each subfolder has a pdf file inside and they all have to be renamed to carry the same name. now they are named with continuous numbers.
I have been doing all my stuff with the automator as I am a novice to scripting. but i kinda don't like giving up on a problem. googling did not bring any good solutions.
i have tried to make sense of example scripts and amend them, but had no success.
a short try was:
tell application "Finder"
set selected to selection
open selected
get files of folders of selected
set name of files of folders to "anschreiben"
end tell
I have also tried:
tell application "Finder"
set selected to selection
open selected
set mlist to every folder of selected
set current_folder to first item of selected
set xxx to first item of current_folder
set name of xxx to "yyy"
repeat with this_folder in mlist
open current_folder
set item 1 to item of current_folder
end repeat
end tell
, but this renamed the first folder to yyy and produced and error that the file name is already given (because it renames the subfolders, not the files inside)
SO...how do I get on level deeper and rename all the files in the subfolders??
Thanks guys, I know it's probably easy for you.
Look for "entire contents"
set newName to "New.pdf"
tell application "Finder"
set myFiles to every file of (entire contents of (first item of (selection as alias list)))
repeat with aFile in myFiles
set aFile's name to newName
end repeat
end tell
it is done! Thanks to you and one or two other pros I now have a beautiful bulk mailing script routine using automator, a bash line and (mainly) applescript. I use it for job applications but you can use it for any case where you want individualised bulk emailing with Mail, MS Word and any given list of contacts in Excel (or Address Book for that matter). For the sake of being complete I will add all necessary steps. with any given list of x names, email addresses, personal addresses you can generate x subfolders, containing x personalized letters and not-personalized documents. once you start the last script and select the folder you can watch mail sending them all out, addressing the person by name and attaching the right personalized letter (you were involved in this part, adayzone!)! It corrects for foreign name spelling that is rendered differently in the email address. It works best for email addresses using the last name before the "#" and can now ignore the first name if it is set in front of the last name (i.e. firstname.lastname#company.com). Thank you all very much for the assistance! this was great team effort.
I shall post it as soon as I am home, should I add it in here or in another forum (for sharing code)?
I'm starting to poke around with Applescript and am looking at writing a few scripts for managing windows. A common task they will all need is to get the current screen size.
I've created a screen_size subroutine that seems to work, and I want to be able to share that with all my scripts. However, I can't figure out a way to put that in a separate file that I can load in my other scripts. I tried creating a separate screen_size.scpt file and use load script "screen_size.scpt", but I get an error about "can't make "screen_size.scpt" into a type file".
There has to be a way to do this, but I haven't been able to find anything online about how to do it.
EDIT:
The POSIX stuff suggested isn't working for me. I'm able to create the file object, but it refuses to convert to an alias, saying it can't find the file (looks like the POSIX file stays relative instead of expanding fully).
I found a suggestion online to use Finder, and have gotten the following working to get an alias:
tell application "Finder"
set _myPath to container of (path to me) as text
end tell
set _loadPath to (_myPath & "screen_size.scpt")
set _loadAlias to alias _loadPath
However, the next line fails with a syntax error, claiming that _loadAlias isn't a variable:
property _ScreenSize : load script _loadAlias
Every variation of this I've tried (doing the alias in the load call, etc) fails, always claiming the variable doesn't exist, even though I know it's being set and working as I can display it. What's going on? Why is it claiming a variable doesn't exist when it obviously does?
AppleScript is doing some really weird things when saving and I haven't figured out what's going on, but I ended up getting something to work.
Here's what I have:
on load_script(_scriptName)
tell application "Finder"
set _myPath to container of (path to me) as text
end tell
set _loadPath to (_myPath & _scriptName)
load script (alias _loadPath)
end load_script
set _ScreenSize to load_script("screen_size.scpt")
set _bounds to _ScreenSize's screen_size()
-- ...
The other answers were saying to set _ScreenSize as a property, but that would cause a syntax error which prevented me from ever saving the file. When I did it just using set it worked.
I wasn't ever able to get the POSIX path stuff suggested to work, but poking Finder for the path worked fine.
In order to execute an action from another script, you'll have to create an handler in the script you're going to load (in your answer you already did this with "screen_size()".
In your case this script will be "screen_size.scpt".
So "screen_size.scpt" will have to look something like this:
on screen_size()
--your actions
return [yourvalue] --the value you want to pass to the other script
end screen_size()
The script you'll load it from will have to look like this:
tell application "Finder"
set _myPath to (container of (path to me) as text & "screen_size.scpt") as alias
end tell
set _ScreenSizeScript to load script _myPath
set _bounds to _ScreenSizeScript's screen_size()
If it doesn't work, or you don't understand me completely, feel free to ask (:
Yes there is a way to do this. Load the file into a property and access it that way
property _ScreenSize : load script (alias "pathtoscript")
_ScreenSize's doStuff()
and for relative paths try this:
set p to "./screen_size.scpt"
set a to POSIX file p
so perhaps this will work:
set p to "./screen_size.scpt"
set a to POSIX file p
property _ScreenSize : load script (alias a)
_ScreenSize's doStuff()
I have people using my libraries on a daily basis, so I first ensure the library is here before calling it.
Let's say I have a library "Lib.Excel.app" (save as non-editable application with Satimage's Smile).
At the beginning of a script that makes use of it, I "load" the library by using this code :
set commonCodeFile to (path to library folder as string) & "Scripts:CommonLibraries:Lib.Excel.app"
tell application "Finder"
if not (exists (file commonCodeFile)) then error ("\"Lib.Excel\"
" & "
should be found in folder
" & "
scroll > CommonLibraries")
end tell
global cc -- make it short and easy to write :)
set cc to load script alias ccFile
Then when I have to use a function from the lib, I just call it like this :
set {what, a} to cc's veryNiceFunction()
Yes you can. You need the full path to the script however.
I believe you can still use "path to me" to get the path to the app executing the current script, and you can then modify that path to point to your sub-folder containing the scripts.
I used this technique to get around AppleScripts (former) 32k text size limits years ago for some really large/complex IRC scripting.
I think I still have all those old scripts in my G4, which is under the desk in my office at work. Sadly it's behind a Enet switch and I can't VNC into it otherwise I'd have tons of sample code to post.
You CAN load the script in a variable, but you have to declare it first.
property _ScreenSize : missing value
tell application "Finder" to set _myPath to container of (path to me) as text
set _loadPath to (_myPath & "screen_size.scpt")
set _loadAlias to alias _loadPath
set _ScreenSize to (load script _loadAlias)