How do I copy files with two properties in AppleScript - properties

I have an external device in my Mac and want to copy the last updated file of this USB-device to my Mac (for example a .PDF or a .jpg). But I have alway an .ini-file (which is alway updated last) on this device and don't want to delete this. I have tried to following code, but it does not work
property source : "Folder A:"
property destination : "Macintosh HD:Users:User:Desktop:Folder B:"
with timeout of (30 * 60) seconds
tell application "Finder"
set the_file to last item of (sort (get files of folder source whose kind ≠ ".INI") by modification date)
copy the_file to folder destination
end tell
end timeout
Another try was to opposite, but it does not work as well
property source : "Folder A:"
property destination : "Macintosh HD:Users:User:Desktop:Folder B:"
with timeout of (30 * 60) seconds
tell application "Finder"
set the_file to last item of (sort (get files of folder source whose kind = ".pdf") by modification date)
copy the_file to folder destination
end tell
end timeout

This following AppleScript code should work for you. Your code was real close. I don’t think it’s necessary to explain what I did because I think it’s obvious by just looking at my version, the few changes that I made.
Sorry about editing your variable names. I took the liberty to rename the variables with what is called “Camel Case”. You can always change them back to your original names if you prefer.
property sourceFolder : "Folder A:" -- The Full Path To This Folder Needs To Be Defined
property destinationFolder : "Macintosh HD:Users:User:Desktop:Folder B:"
property nameExtensions : {"jpg", "pdf"}
with timeout of (30 * 60) seconds
tell application "Finder"
set theFile to last item of (sort (get files of folder sourceFolder ¬
whose name extension is in nameExtensions) by modification date)
copy theFile to folder destinationFolder
end tell
end timeout

Related

Applescript: Recursive Folders of Selected Folder

Using applescript, I'm trying to create a .patch file in a set of parallel folders for every .recipe file in a target folder and its sub-folders. This also includes creating a parallel folder structure as the script does it's thing. I've got the steps right, but some part of the detailed grammar is wrong and I can't figure out what and how to fix it. Debugging information I've read so far has not helped me, though I'm sure this is a simple matter of incorrect applescript grammar. Here is the code:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
on run
set patchFile to (choose file with prompt "Select patch file...")
set assetsFolder to (choose folder with prompt "Select recipe assets folder...")
set dumpFolder to (choose folder with prompt "Select dump folder...")
makePatches(patchFile, assetsFolder, dumpFolder)
end run
on makePatches(patchFile, targetFolder, dumpFolder)
tell application "Finder"
set recipeCount to count (files in folder (targetFolder) whose name ends with ".recipe")
if (recipeCount > 0) then
set recipeFiles to files in folder (targetFolder) whose name ends with ".recipe"
repeat with eachFile in recipeFiles
set recipePatch to duplicate patchFile to folder dumpFolder
set recipePatch's name to eachFile's name & ".patch"
end repeat
end if
set folderCount to (folders in folder (targetFolder) whose visible is true)
if (folderCount > 0) then
set subFolders to folders in folder (targetFolder)
repeat with eachFolder in subFolders
set newName to folder eachFolder's name
set newFolder to make new folder at folder dumpFolder
set newFolder's name to newName
my makePatches(patchFile, eachFolder, newFolder)
end repeat
end if
end tell
return
end makePatches
Note that if I remove the code for iterating through the folders and simply have it create patches for each .recipe file in a folder, then that code works. Or at least, it does as far as I can tell. So the problem should be somewhere at or after folderCount. The recursion is needed, as is the ability to use choose with prompt. Hopefully that isn't a problem. What is the correct grammar for the iteration section?
After semi-blindly adjusting the code and looking at just about every relevant example I could find, I came up with some code that does exactly what is needed. But I don't really understand what was going wrong with the original, so while I'll post the working code I made, I'd still like to hear what was wrong with the original code explained.
on run
set patchFile to (choose file with prompt "Select patch file...")
set assetsFolder to (choose folder with prompt "Select recipe assets folder...")
set dumpFolder to (choose folder with prompt "Select dump folder...")
makePatches(patchFile, assetsFolder, dumpFolder)
end run
on makePatches(patchFile, targetFolder, dumpFolder)
tell application "Finder"
set recipeFiles to files of targetFolder whose name ends with ".recipe"
repeat with eachFile in recipeFiles
set recipePatch to duplicate patchFile to dumpFolder
set recipePatch's name to eachFile's name & ".patch"
end repeat
end tell
tell application "Finder" to set theseSubFolders to folders of targetFolder
repeat with tSubF in theseSubFolders
set newName to tSubF's name
tell application "Finder" to set newFolder to (make new folder at dumpFolder with properties {name:newName})
my makePatches(patchFile, tSubF, newFolder)
end repeat
end makePatches

DateCreated glitch? How to identify if overwriting

As part of an auto-update macro, I have a copy of an Access FrontEnd on the local drive which when opened, checks to see if the file on the server has a newer CreatedDate. If it is newer, my code currently uses fileSystemObject to CopyFile and overwrites the local drive version (filename remains the same).
The problem I am having however is that when this is done, the 'date created' does not change to that of the newer file and so continually loops as the old file is closed and the new one opened - which checks for updates based on date created... I even tried using kill on the local file and waiting 10 seconds before doing the Copyfile command but even then it appears with the deleted file's creation date.
When I get back to the office I'm going to try copying the file from the server to the local drive without renaming it, deleting the original local drive file, then copying the newly created file so I can rename it back to the original name (the name should not be changed so that any shortcuts will still work).
Has anyone come across this before and found a solution? Am I missing something obvious that would 'refresh' the created date of the file?
EDIT
I think this is pretty close to what I had.
Dim strSource As String, strDest As String, strOrigDB As String, strSvrDB As String
Dim varOldDB As Variant, varNewDB As Variant, fso As Object
Set fso = CreateObject("scripting.filesystemobject")
strSource = "\\192.168.1.2\Data\svrDatabase"
strSvrDB = "AnyOldName.mde"
strDest = "C:\myFolder\myDatabase"
strOrigDB = "KeepMyName.mde"
varOldDB = (strDest & "\" & strOrigDB)
varNewDB = (strSource & "\" & strSvrDB)
If fso.getfile(varOldDB).DateCreated < fso.getfile(varNewDB).DateCreated Then
Kill strDest & "\" & strOrigDB
Excel.Application.Wait Now() + TimeValue("00:00:05")
fso.copyfile (strSource & "\" & strSvrDB), (strDest & "\" & strOrigDB), True
'open new database version
ShellExecute 0, "Open", (strDest & "\" & strOrigDB), "", "", 1
DoCmd.Quit
End If
I was originally using date modified, but noticed that as soon as the Front end opened, this would get renewed and would therefore always be a newer date value than the server file.
EDIT
After thinking about this, and hoping my logic hasn't completely broken down, it would be best if I had a shortcut for the user to click on, but instead of opening the Frontend, it opens a script file that checks for an update. If there is an update, then it deletes the local Frontend and copies the server one (which should be named differently to the original) to the local folder - then opens the new frontend.
This would mean that the datecreated being checked is always going to be updated on copy to the local folder and the kill will work because the file has not been opened. I am still a little wary of using lastdatemodified incase a user has the database open when an update is created - the Frontend contains tables used in searches which I believe will alter the modified date of the Frontend. In this scenario the local date modified would still be greater than a new frontend on the server.
You should be using Date Modified. That's when the file was last changed.
This is what helps says about times.
Timestamps are updated at various times and for various reasons. The only guarantee about a file timestamp is that the file time is correctly reflected when the handle that makes the change is closed.
So try closing all files before comparing times.
Also From https://support.microsoft.com/en-us/kb/172190
When a name is removed from a directory (rename or delete), its short/long name pair and creation time are saved in a cache, keyed by the name that was removed. When a name is added to a directory (rename or create), the cache is searched to see if there is information to restore. The cache is effective per instance of a directory. If a directory is deleted, the cache for it is removed.
Also coping from an untrusted source will change last modified for sure.
Also there are rules for copy versus move.
Also files need to be wholey within Windows eco system for windows rules to apply for sure.

Applpescript: Reading Text file for List of Files

I have been trying to cobble together a script to take a list of files from a text document and have applescript go through the list line by line and then do something with the file. In this case it is changing the label on the file, but we would use it in other instances to move files, etc.
It works with a test file on my desktop the files get marked with the purple Label, but when trying to run it in the folder I actually need to it fails with this error message:
error "Finder got an error: Can’t set 1 to 5." number -10006 from 1
The text files are the same except for the length of their content.
Could this be a an issues with filenames, and if so how do I make the script more tolerant.
Here is the script:
set textFileContents to POSIX path of (choose file)
set dataList to paragraphs of (read textFileContents as «class utf8»)
tell application "System Events"
repeat with thisFileName in dataList
tell application "Finder" to set (label index of files whose name is thisFileName) to 5
end repeat
end tell
Any help would be appreciated, thanks.
1080074 3.tif
1080074 2.tif
1080069_A1.tif
Here is the final code from the solution to this problem and some further work I did.
Thanks to #Mark Setchell & #jackjr300 for all of their patient help.
set justpath to POSIX path of (choose folder with prompt "Select the Folder with Files You Want to Use")
set textFileContents to (choose file with prompt "Select the list of files")
set dataList to paragraphs of (read textFileContents as «class utf8»)
tell application "Finder"
repeat with FileName in dataList
try -- need a try block to ignore error, in case that the file has been moved or deleted
set label index of (justpath & FileName as POSIX file as alias) to 5
end try
end repeat
end tell
You seem to have a spurious tell application "System Events" in there. It works like this:
set FileName to POSIX path of (choose file)
set FileRecords to paragraphs of (read FileName)
repeat with ThisFileName in FileRecords
say ThisFileName
tell application "Finder" to set (label index of files whose name is thisFileName) to 5
end repeat
Note that my test file isn't UTF8.
Update
By the way, if all you want do is set the label colour on some files, it may be easier to do that from the Terminal and not worry with Applescript. Say you start the Terminal, and go to your Desktop like this
cd Desktop
you can then change the labels of all files on your Desktop (and in any subdirectories) whose names contain "Freddy" followed by "Frog" (i.e "fileForFreddyFrog.txt", "file from Freddy the Frog.php")
find . -name "*Freddy*Frog*" -exec xattr -wx com.apple.FinderInfo "0000000000000000000700000000000000000000000000000000000000000000" {} \;
You need to specify the folder because the finder has no current folder, except the Desktop if you don't specify a folder.
set textFileContents to choose file
set dataList to paragraphs of (read textFileContents as «class utf8»)
set theFolder to "/Users/jack/Desktop/xxx/yyy" as POSIX file as alias -- change it to the path of your folder
tell application "Finder"
repeat with thisFileName in dataList
try -- need a try block to ignore error, in case that the file has been moved or deleted
set label index of file thisFileName of theFolder to 5
end try
end repeat
end tell
Change the path in the third line of this script
--
If the text file contains the full path of the file, you can use this script.
set textFileContents to choose file
set dataList to paragraphs of (read textFileContents as «class utf8»)
tell application "Finder"
repeat with thisPath in dataList
try -- need a try block to ignore error, in case that the file has been moved or deleted
set label index of (thisPath as POSIX file as alias) to 5
end try
end repeat
end tell

How can I create a display dialog that presents specific file path options for a given PDF file?

I am very new to using Automator and Applescript.
I would like to use Automator and AppleScript to detect PDF files that are downloaded to the "Downloads" folder and opens a display dialog that allows me to select the file path and move the file. So far, what I have (which isn't right) is something like:
set question to display dialog "Save fileName in..." buttons {"Figuring Relation", "Iconoclasm", "Elsewhere"} default button 3
set answer to button returned of question
if answer is equal to "Figuring Relation" then
tell application "Finder" to move fileName to POSIX file "/Users/mac/Documents/College/Junior/Fall/Art 347 - Figuring Relation"
I want the "Figuring Relation" and "Iconoclasm" buttons to change the file path to a designated file path (I don't want to browse for it), and the "Elsewhere" button to open a Finder window where I can select/browse the path.
If possible, I'm also looking to add the date to the beginning of the file name as "mm-dd_filename".
I am not sure of how to translate the Automator Input to Applescript, or how to include the filename in the display dialog text. Thank you so much for any help.
Here is an example using just applescript. In my example, it assumes you're selecting the file you're wanting to move, but you could easily add something for the script to "Find" all files ending with ".pdf" if you wanted to and then loop through the results.
on run
try
set thisFile to choose file
tell application "Finder" to set currentName to thisFile's name
-- Setting variables for the destinations to be used later
set FiguringRelationPath to (path to documents folder) & "College:Junior:Fall:Art 347 - Figuring Relation:" as string
set IconoclasmPath to (path to documents folder) & "Iconoclasm:" as string
-- Ask the user
set answer to button returned of (display dialog "Save \"" & currentName & "\" in..." buttons {"Figuring Relation", "Iconoclasm", "Elsewhere"} default button 3)
-- Set the destination variable based on the users response to the dialog
if answer is equal to "Figuring Relation" then
set destination to FiguringRelationPath
else if answer is equal to "Iconoclasm" then
set destination to IconoclasmPath
else
set destination to choose folder with prompt "Please select the destination folder" as string
end if
-- Test that the destination directory exists, if not post the error
try
set destination to destination as alias
on error
error ("Destination path " & destination as string) & " doesn't appear to exist"
end try
-- Rename the file with the date prefix
set tDatePrefix to (do shell script "date '+%m-%d'") & "_" as string
tell application "Finder" to set x's name to tDatePrefix & x's name as string
-- Move the file
tell application "Finder" to move thisFile to destination
on error err
activate
display dialog "Error: " & err buttons {"OK"} default button 1
end try
end run

Delete PDF files only from folder using Applescript on MAC

I need to delete the PDF files only from a folder on a network drive. There are other file types in the folder I can not touch. I am assuming I have to choose or identify the PF files first and then move them to the trash or delete them.
What I have so far:
tell application "Finder"
set theFolder to "Macintosh HD:Users:Kathlene:Desktop:ABC123_JOB"
set destFolder to "Server/JOBS/TRANSFER_TRASH/"
set thePDFs to every file of theFolder whose name extension is "pdf"
move thePDFs to destFolder
end tell
What I get for an error:
error "Can’t get every file of \"Macintosh
HD:Users:Kathlene:Desktop:ABC123_JOB:\"." number -1728 from every file
of "Macintosh HD:Users:Kathlene:Desktop:ABC123_JOB"
Try:
tell application "Finder" to delete (files of folder "Macintosh HD:Users:Kathlene:Desktop:ABC123_JOB" whose name extension is "pdf")