So I have a folder with many files whose count changes regularly and I need to count them using AppleScript. I had a script like this in place at first:
tell application "Finder" to return count of (every file of (choose folder))
Later, while researching, I found something like this:
tell application "Finder" to return count files of (choose folder)
However neither seems to work. I can't use System Events either since the choose folder just results in an endless run loop when used inside a System Events-tell block.
The error I get is:
"Finder got an error: Expected a reference." number -1727
If you are looking to get just the count of files within the selected folder and not subfolders or files within subfolders, then use:
tell application "Finder" to return count files of container (choose folder)
If you want the full count of items within a folder, then use:
tell application "Finder" to return count items of entire contents of container (choose folder)
Related
First, I ran the script code in the script editor, and found that there was an error. The Numbers did not understand the save command, The script command was as follows:
tell application "Numbers"
set thisDocument to open alias "Macintosh HD:Users:admin:numbers-FATP.xls"
tell thisDocument
save thisDocument in file "Macintosh HD:Users:admin:NumbersTest.numbers"
end tell
close thisDocument
end tell
And the specific error information was as follows:
error "“Numbers” encountered an error:“remove id \"C76B3CB0-D007-46C4-BEB0-9C65D0E65767\"” do not understand “save” information." number -1708 from remove id "C76B3CB0-D007-46C4-BEB0-9C65D0E65767"
The point is, sometimes can execute the script code, sometimes can't perform, and it is puzzling, I try to change the computer version and version Numbers, but did not solve the problem, this is why, look forward to your reply, best regards!
I refer to this website: https://iworkautomation.com/numbers/document-save.html
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.
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 need some way to determine if a particular file exists. If it exists do one script if not then do another script. Here was my logic in applescript:
If exists "File:Path:To:theFile"
tell application "Finder"
open "File:Path:To:the:script"
end tell
else
tell application "Finder"
open "File:Path:To:the:Anotherscript"
end tell
end if
The only problem is that sometimes when i use the above logic the script fails saying can't find the file. I need a full proof, never fails way to see if a file exists. I'm open to using the terminal, or applescript. I'm sure someone has run into this before but I have looked all over the web for an answer but could not find one.
In your original code you're giving the exists function a string, rather than a file, even though it's the path to a file. You have to explicitly give it a file, or it treats it the same as if you had tried to do
exists "god"
or
exists "tooth fairy"
The exists command won't know what you're talking about. You could use
return exists alias "the:path:to:a:file"
but aliases don't work unless the file actually exists, so a non-existent file will create an error. You could of course catch the error and do something with it, but it's simpler to just give the exists function a file object. File objects belong to the Finder application, so:
return exists file "the:path:to:a:file" of application "Finder"
I use the following to see if an item in the Finder exists:
on FinderItemExists(thePath)
try
set thePath to thePath as alias
on error
return false
end try
return true
end FinderItemExists
I think what you're missing is conversion of the path to an alias.
This sounds like a good place for a try...on error block. I believe the following should do what you want:
tell application "Finder"
try
open "File:Path:To:the:script"
on error
open "File:Path:To:the:Anotherscript"
end try
end tell