I created a droplet which would allow me to rename files from an input box.
on adding folder items to este_folder after receiving este_file
display dialog "what's their name?" default answer ""
set text_returned to text returned of the result & ".jpg"
display dialog text_returned
tell application "Finder"
set the name of file este_file to text_returned
end tell
end adding folder items to
It works fine, but it creates a loop where I have to hit cancel again to stop the script, as it thinks a new file has been added. I would like to just rename it once; and then not have the second dialog box pop up again. I have tried rerouting the file to another folder:
on adding folder items to este_folder after receiving este_file
display dialog "what's their name?" default answer ""
set text_returned to text returned of the result & ".jpg"
display dialog text_returned
tell application "Finder"
set the name of file este_file to text_returned
end tell
repeat with anItem in este_file
tell application "Finder"
set destFolder to "Macintosh HD:Users:maxwellanderson:Desktop:BetterinTexas" as alias
move anItem to folder destFolder
end tell
end repeat
end adding folder items to
But that doesn't work either-it doesn't process the renaming portion of the script. Any suggestions on what I should do to get rid of the second dialog box?
The script is being called twice because renaming the file within the watched folder is—for all intents and purposes—like adding a new file into the folder. Therefore, it's called once when the file is actually added; and called a second time when it's renamed.
Moving the file as you suggested will work, but you have to move the file before you rename it. Therefore, shove the code that moves the file near the top of the script, then the renaming bits at the bottom.
As a side-note, I notice you have a repeat with loop to handle multiple file moves, but only one statement that handles a single file renaming. One of these is not like the other. If this watched folder received multiple files at the same time, it would most likely rename them all to the same name, thus potentially over-writing multiple files. If the watched folder only ever receives one file at a time, anyway, then the repeat with loop is redundant.
This code is modelled on yours and will handle a single file move-and-rename (but not a group of files—or, more accurately, as I stated above, it would rename multiple files to the same name, thus overwriting all but the last one in the list):
on adding folder items to este_folder after receiving este_file
set destFolder to POSIX file "/Users/maxwellanderson/Desktop/BetterinTexas" as alias
set text_returned to text returned of ¬
(display dialog "what's their name?" default answer "") ¬
& ".jpg"
display dialog text_returned
tell application "Finder" to ¬
set the name of ¬
(move file este_file to destFolder) ¬
to text_returned
end adding folder items to
If you need it to handle multiple files, then you can wrap everything from set text_returned to to text_returned in a repeat with loop as you've done in your second code block. This will sequentially bring up dialog boxes—one per file—and move/rename the files accordingly.
If you have any questions, or need clarification, leave a comment and I'll get back to you.
Related
I worked out two scripts to do that, but none of them is working (I even don't get shown the "on error" message. Maybe it's because of the pre-defined folder as a variable? As for simplicity and a cleaner code I didn't want to write the complete path again, so I used that folder variable. In case it is relevant, the DataHubFolder variable was defined like this earlier: set DataHubFolder to cloudDocs & "Current Projects:Data Hub:"
Here are the codes I used:
Code #1:
try
tell application "Finder"
set f to every file ¬
of folder ¬
of DataHubFolder & "Distrokid:"
delete f
end tell
on error
display dialog ("Error. Couldn't move the file") buttons {"OK"}
end try
Code #2:
try
tell application "Finder"
delete (every item of folder (DataHubFolder & "Distrokid:"))
end tell
on error
display dialog ("Error. Couldn't move the file") buttons {"OK"}
end try
Would be so happy about some tips.
Thanks a lot.
As you can see from my code below I am extremely new to this. My code just about works, but my major issue is that it hogs up Finder and sometimes it does not set the Desktop picture, but does most of the time!
The script just monitors a folder, and if an "***.jpg" is added then the Desktop picture set to it.
This is my very first script so I have a lot to learn,
set reset to ""
display notification "Alarm Front Active " & (current date) as string
tell application "Finder"
set path_to_sourceFull to ":photo:FRONT CAM 1:20190929:images" -- from nsa310 network drive
set path_to_source to ":photo:FRONT CAM 1:20190929:images" -- from nsa310 network drive
set directory1 to "/Volumes/photo/FRONT CAM 1/20190929/images" as text -- from nsa310 network drive
set path_to_destinationFull to "Macintosh HD:Users:rekordbox:Documents:temp folder 2"
set path_to_destination to ":Users:rekordbox:Documents:temp folder 2"
set directory2 to "/Users/rekordbox/Documents/temp folder 2" as text
repeat while reset = ""
set allok to ""
set filelist to name of every item in folder path_to_source --of startup disk
set listSizesaved to count of filelist
delay 1
repeat while allok = ""
set filelist to name of every item in folder path_to_source --of startup disk
set listSize to count of filelist
if listSize = listSizesaved then
else
set filelist to name of every item in folder path_to_source --of startup disk
set listSize to count of filelist
set LastAddedFile to item listSize of filelist
set allok to "ALARM"
set listSizesaved to listSize -- (save the updated) count
set activefile to (path_to_source & LastAddedFile)
set selectedpicture to (directory1 & "/" & LastAddedFile)
tell application "System Events" to tell every desktop to set picture to selectedpicture
delay 1
display notification "ALARM FRONT TRIGGERED...." & (current date) as string
delay 1
end if
end repeat
end repeat
end tell
The script you want, I think, is this:
on adding folder items to thisFolder after receiving filelist
set droppedFile to first item of filelist
tell application "System Events"
tell every desktop
set picture to droppedFile
end tell
end tell
end adding folder items to
(I've left out the 'Alarm' bit, since I wasn't sure what the point of it was.)
To use this script, copy it into Script Editor, save it in the folder ~/Library/Scripts/Folder Action Scripts/, then open the applet 'Folder Actions Setup'. Add the folder you want on the left-hand side, and choose the file you just saved on the right. It should look something like this:
...where the checkmark on the left shows that folder actions are enabled for the folder (which I called 'test folder') and the script (which I called 'FADtop.scpt') is attached.
Drop an image in the folder, and it should just work.
As a general rule, don't script the Finder unless you absolutely need to; always use System Events. The Finder is a busy app, and scripting it can gum up the system. And also try to avoid this design pattern:
(* Don't do this! *)
repeat
(* test for something *)
delay x
end
The delay command is not particularly resource-efficient. If you really want to use a polling system to test for some event, it's often better to create a stand-alone app with an on idle handler. That way you let the system wake and sleep the script, with significant performance improvements.
EDIT
Since folder actions don't seem to be working with ftp drops onto remote drives, here's a reasonably efficient folder-polling approach. Save the following script as a stay-open application (choose 'Application' as the file type, and click the 'stay open' checkbox). Then launch the application and leave it running in the background.
property dateOfLastFileChosen : missing value
property targetFolder : "/Volumes/photo/FRONT CAM 1/20190929/images"
property idleTime : 300 -- 300 seconds is five minutes
on run
end run
on idle
tell application "System Events"
if exists folder targetFolder then
if dateOfLastFileChosen is missing value then
set recentFiles to every file of folder targetFolder whose visible is true
else
set recentFiles to every file of folder targetFolder whose modification date > dateOfLastFileChosen and visible is true
end if
set newFile to my mostRecentFileOfList(recentFiles)
if newFile is not missing value then
set dateOfLastFileChosen to modification date of newFile
tell every desktop
set picture to (POSIX path of newFile)
end tell
end if
end if
end tell
return idleTime -- check every 5 minutes (300 seconds)
end idle
on mostRecentFileOfList(fileList)
set maxDateObj to missing value
repeat with thisFile in fileList
if maxDateObj is missing value then
set maxDateObj to contents of thisFile
else if modification date of thisFile is greater than modification date of maxDateObj then
set maxDateObj to thisFile
end if
end repeat
return maxDateObj
end mostRecentFileOfList
Without trying to steal the thunder from #Ted Wrigley, whose solution provided the AppleScript code for the folder action, I felt there were enough comments and items for me to add to post it as another answer to the OP's dilemma.
First I will address the tell every desktop set picture to droppedFile lines of code in the following AppleScript Folder Action. If the user has only one monitor/display attached to the computer, but has created several different "Spaces", the tell every desktop set picture to droppedFile lines of code will only change the Desktop Picture for the Desktop of the current active "Space" only. The other Desktop backgrounds will not be changed. However, if the user has several monitors/displays attached to the computer, the tell every desktop set picture to droppedFile lines of code will change the Desktop Pictures for the Desktops of the current active "Space" for each attached monitor/display. If the latter is not the desired result, then tell every desktop should be changed to tell current desktop.
After testing the AppleScript Folder Action code provided by #Ted Wrigley, I noticed the image file being downloaded from an FTP server, to the test folder where I have the folder action script attached to, looked like this before the image was actually finished transferring. Because the file was kind of there and not there, it did not trigger the Folder Action.
Next, I figured I would add a delay to be beginning of the Folder Action code to allow for the transfer of the image file from the FTP server, to complete. I added a delay of 180 seconds to allow for the transfer to complete and it worked. When the transfer was complete, the file look like this.
Depending on how many files you foresee being transferred at any given time along with factoring in for file sizes... It's possible you may need to significantly increase the Delay time.
on adding folder items to thisFolder after receiving theseFiles
delay 180
set newBackground to first item of theseFiles
tell application "System Events"
set picture of current desktop to newBackground -- Single Display Attached
--set picture of every desktop to newBackground -- Multiple Displays Attached
end tell
end adding folder items to
Objective: Move all files and files in folders to destination folder and maintain the file structure [files and named folders]. Important for music files in albums.
Functional: Move all listed files in SmartFolder [named] to destinationFolder with serial / consecutive move operation and maintain the same file structure and copy of dataFiles listed in SmartFolder.
Key: All files were obtained for transfer. Normal CMD + A, CMD + C, CMD + V hangs up the computer and the transfer does not initiate. The AppleScript to move each dataObject to destinationPath is all.
Facts: How to reference objects [files, folders] and their proper reference format and accepted path syntax; path or POSIX, and use of alias. Basic operations. I ran an AppleScript to move filePath to pathDestination, and was otherwise successful, and would be nice to known the formalization syntax for path reference.
tell application "Finder"
move allFiles to destinationFolder
// recursive/repeat code to loop through all listed files and folder
end tell
Reference: Applescript, show all files with tag
[Moving / selecting listed files from 'smartfolder' containers as active windows and displayLists. It was an alt. solution since AppleScript will not reference the SmartFolder as an object, nor will it dynamically call the listProperty of the SmartFolder object unless called by an unknown or un-reference method or command.
Since your main issue, as far as I can tell, seems to be dealing with SmartFolders in AppleScript, which—as you said—cannot be referenced as folder objects, this little snippet might be of help:
set SmartFolder to "/Users/CK/My Smart Folder.savedSearch"
tell application "System Events" to get value of property list file SmartFolder
set {[Scope], Query} to {SearchScopes, RawQuery} of RawQueryDict of result
set AppleScript's text item delimiters to tab
set Command to {"mdfind -onlyin", ¬
quoted form of Scope as text, ¬
quoted form of Query as text} as text
set SearchResults to paragraphs of (do shell script Command)
--> returns a list of posix files
--> e.g. {"/Users/CK/Downloads/This is file one.txt", ...}
This will return a list of POSIX paths to files that match the search criteria.
I would recommend using System Events rather than Finder to deal with large numbers of files. It can also handle posix paths without the need to manually coerce them into aliases or whatever. So, given a posix path, such as the ones returned in the list using the above code snippet, you simply do this:
set myfile to item 1 of SearchResults
tell application "System Events" to move myfile to "/Users/CK/Desktop"
Without knowing more details of what your smart folder contains (since some searches could easily return a folder plus the contents of that folder, which you'd have to bear in mind when getting your AppleScript to recurse through the search results), I can't give you more than that. But, you said your main problems were being unable to handle SmartFolders and not knowing how to reference files/folders.
This works for me using the latest version of Sierra.
Set the value of property moveToNewFolderto the destination folder of your choice
This script creates a "Choose From List" Dialog, allowing you to choose any smart folder which resides on your system. It will then move all of these files and folders in the chosen smart folder, to your set destination folder.
property savedSearches : (path to home folder as string) & "Library" & ":Saved Searches"
property savedSearchesSubFolders : {}
property namesOfSavedSearchesSubFolders : {}
property selectedSearchFolder : ""
property selectedSearchFolderPath : missing value
property moveTheseItems : missing value
property moveToNewFolder : (path to desktop as text) & "untitled folder" -- change this value to your preferred destination folder
tell application "Finder"
activate
delay 0.1 -- may need to adjust delay time value
open savedSearches
delay 0.1 -- may need to adjust delay time value
reveal savedSearches
delay 0.1 -- may need to adjust delay time value
select savedSearches
delay 0.1 -- may need to adjust delay time value
set current view of Finder window 1 to column view
delay 0.1 -- may need to adjust delay time value
tell its Finder window (POSIX path of savedSearches)
delay 0.1 -- may need to adjust delay time value
set savedSearchesSubFolders to items
set namesOfSavedSearchesSubFolders to name of items
-- Allows to choose any smart folder from a list of all smart folders
set selectedSearchFolder to choose from list namesOfSavedSearchesSubFolders ¬
with title ¬
"Smart Search Folders" with prompt ¬
"Choose Your Folder" OK button name ¬
"OK" cancel button name "CANCEL"
set selectedSearchFolder to selectedSearchFolder as text
set selectedSearchFolderPath to savedSearches & ":" & selectedSearchFolder
set selectedSearchFolderPath to selectedSearchFolderPath as string
end tell
delay 0.2 -- may need to adjust delay time value
select selectedSearchFolderPath
tell Finder window 1
set defaultView to current view
set current view to list view
delay 0.1 -- may need to adjust delay time value
set current view to defaultView
delay 0.5 -- may need to adjust delay time value
tell application "System Events"
key code 0 using command down
end tell
end tell
set moveTheseItems to selection
end tell
tell application "Finder"
set resultObject to move moveTheseItems ¬
to moveToNewFolder ¬
with replacing
end tell
I'm creating an automator pdf print plugin.
When you choose the print plugin the filename to the pdf is the input (normally /var/something /documentName.pdf)
I would like to get the documentName to use it later in an Rename Finder Item.
I'm using atm applescript to accomplish this.
on run {input, parameters}
tell application "Finder"
set fileName to name of ((POSIX file input) as alias)
end tell
return fileName as string
end run
The problem is that this only works when I put an Ask for Text Action before the applescript which displays the posix path.
If I remove the Ask for Text action the applescript fails.
The workflow is at https://www.dropbox.com/s/jp4t9pen3gvtyiq/Rename-Action.workflow.zip
I guess it is something simple but this is the first applescript / automator workflow I'm creating.
As I fail on commenting
Solution is
on run {input, parameters}
tell application "Finder"
set fileName to ((name of first item of input) as string)
end tell
return fileName
end run
as by #Ken post below.
Thanks!
I created a test workflow with this AppleScript:
on run {input, parameters}
tell app "System Events"
display dialog ((class of input) as string)
end
return input
end run
That displayed "list". I then modified it to:
on run {input, parameters}
tell application "System Events"
display dialog ((class of first item of input) as string)
end tell
return input
end run
That displayed "alias".
So, the input to a PDF workflow is a list of aliases. Write your script with that in mind and it should work. For example, this works:
on run {input, parameters}
tell application "System Events"
display dialog ((name of first item of input) as string)
end tell
return input
end run
When working with AppleScript, it can really help to forget everything you know about file paths. If you think in paths, you will be doing path-math in your head and all of it is unnecessary work. What you want to work with is objects. When doing file operations, you work with alias objects.
If you look at the PDF you are working with in Finder, and go File ▶ Make Alias then you’ll create an alias file. You can drag that alias file all around the file system of the disk it is on, put it in any folder, and when you open the alias file, it will still always open your original PDF, even if you forget what path name your original PDF file has, and even more importantly: the alias will open the PDF even if the PDF has moved to somewhere else in the file system. An alias does all that work for you. You don’t need to know the path names.
In AppleScript, rather than working with files, you work with aliases, and whatever you do to an alias is also done to the original file. So you don’t need to know the path name of a file to change its name — you only have to have an alias of it to work on. You store that alias in a variable.
So what you want to do is set the input PDF alias to a variable, and then later, that variable is what you give Finder to rename. You don’t have to know any paths. It doesn’t matter where the input PDF is stored on the file system — the alias will take care of that.
Here is an example AppleScript that demonstrates the principle of taking an alias as input, and then later renaming that alias (and thus, the original file:)
tell application "Finder"
set theInputFile to (choose file)
-- do a workflow here
set the name of theInputFile to "Renamed" & "." & the name extension of theInputFile
end tell
Here is a line-by-line description of the above script:
the opening tell block that specifies we are talking to Finder
show the user a choose file dialog box, and set the alias that is returned by that dialog box to a variable called “theInputFile”
a comment that is a placeholder for whatever workflow steps you might want to do
rename theInputFile to “Renamed” and its original file extension
quit talking to Finder
And even where you want to work with the folder that contains your original input file, or want to know what disk the input file is on, you still don’t need to work with path names:
tell application "Finder"
set theInputFile to (choose file)
set theContainingFolder to open the container of theInputFile
set theInputFileDisk to the disk of theInputFile
end tell
And if you want to know what kind of file the input file is, you don’t have to look at the filename extension and figure it out, you can just say:
set theInputFileKind to the kind of theInputFile
if theInputFileKind is equal to "Portable Document Format (PDF)" then
-- do stuff
end if
And if you want to work in specific folders, such as the home folder, there are special properties for that, like “the path to the home folder” so that the following script opens “~/Public/Drop Box” on any system, no matter what the user name:
tell application "Finder"
activate
set theHomeFolder to the path to the home folder as alias
set theDropBoxFolder to folder "Drop Box" of folder "Public" of theHomeFolder
open theDropBoxFolder
end tell
You can walk around disks and folder structures as objects as shown above, so again, there is no need to think in paths. Think in terms of setting variables to objects that you want to interact with.
Solution is
on run {input, parameters}
tell application "Finder"
set fileName to ((name of first item of input) as string)
end tell
return fileName
end run
as by #Ken's post
Thanks!
I'm new to applescript but I want to set up a folder action that:
1. Recognises when a file is added to a folder
2. Tags said folder red
3. Adds a Reminder to the "Downloads" reminder list that has the name of the newly-added file as the body text of the reminder
Using google and Applescript's record function I've frankenscripted this together so far
property dialog_timeout : 30 -- set the amount of time before dialogs auto-answer.
on adding folder items to this_folder after receiving added_items
try
tell application "Finder"
set FILEname to name of (added_items)
set label index of folder "untitled folder" of folder "Desktop" of folder "heyjeremyoates" of folder "Users" of startup disk to 2
end tell
tell application "Reminders"
set mylist to list "Downloads"
tell mylist
make new reminder with properties {name:"D/L Complete", body:FILEname, due date:(current date)}
end tell
end tell
end try
end adding folder items to
Darn thing won't work. Infuriating. I tested it as a folder action with "test" as the name and body of the reminder and it worked fine. I'm pretty sure I've gone wrong somewhere in setting FILEname as the name of the newly copied item because the script as it is now no longer turns the folder red.
The idea behind this is so I can see, from my iPhone/iPad, how many large/scheduled downloads to my home mac (both torrents and large work files - I'll have a seperate folder action and reminder list for each download folder) there are that are yet to be managed.
It seemed like setting up a Growl/Prowl combo was wasteful if iCloud/Reminders and a dozen lines of code could deliver what I wanted anyway. Ideally I'll write a second applescript that will delete the reminder when I rename or move the related file, and though I haven't even thought about how that would work if anyone has any suggestions for it I'd be super grateful
It is a pity you can't (natively) have OSX notifications pushed to an iOS device linked to the same iCloud account though (with the appropriate granularity)
But I digress - can anyone see what I'm screwing up here?
Thanks in advance for even reading this far
added_items is a list of aliases, and name of (added_items) resulted in an error.
on adding folder items to this_folder after receiving added_items
tell application "Finder"
set label index of this_folder to 2
repeat with f in added_items
set n to name of f
tell application "Reminders" to tell list "Downloads"
make new reminder with properties {name:"D/L Complete", body:n, due date:(current date)}
end tell
end repeat
end tell
end adding folder items to
(Save the script in ~/Library/Workflows/Applications/Folder Actions/ and enable the folder action from Folder Actions Setup.)