Hello, my friends! I’m working with videos on an event (so, it’s live!), and I have this complicated scenario:
1- A camera is going to film the same thing several times, and each time it will creates a new file on the folder “01_Camera” on the desktop, for example.
2- This folder “01_Camera” has to be always empty for the new footage that are coming, so each new file has to be moved to the folder “02_Media” and copied to the folder “03_Backup”.
3- The thing is: The folder “02_Media” has to have always ONLY ONE file, named “Film.cine” (because I will use it in a video editor program, refreshing each time)… so every time a new footage comes to the folder “01_Camera”, it has to be moved to the folder “02_Media” and renamed as “Film.cine” (overwriting the old file “Film.cine”).
4- At the same time I have to maintain a copy of each footage file on the folder “03_Backup”
So at the end I have always the folder “01_Camera” empty, the folder “02_Media” with one file (“Film.cine”) and the folder “03_Backups” with all the footage files.
Last thing: because videos are heavy, and I have to make it really fast, i’d like to not make a copy of the files on the folder “01_Camera” to the folder “02_Media”, but moving it while renaming it. On the folder “03_Backup”, I can make a copy because i have no hurry to use these files.
use AppleScript version "2.4" # requires at least Yosemite
use scripting additions
use framework "Foundation"
# Most code borrowed from Shane STANLEY
on adding folder items to this_folder after receiving these_items
set FilmCine to "Film.cine"
set name2 to "02_Media"
set name3 to "03_Backup"
set thePOSIXPath to POSIX path of these_items's item 1
# Get the URL pointing to the folder containing this_folder
set {name1, URLContainer} to my getNameAndContainer:(POSIX path of this_folder)
# Build the URL pointing to the folder "02_Media"
set URL2 to my buildFullPath:name2 inFolder:URLContainer
# Build the URL pointing to the folder "03_Backup"
set URL3 to my buildFullPath:name3 inFolder:URLContainer
# Copy in "02_Media2" as "Film.cine"
my copyThis:thePOSIXPath intoFolder:URL2 withName:FilmCine
# Move to "03_Backup"
my movePath:thePOSIXPath toFolder:URL3
end adding folder items to
#=====#=====#=====#=====#=====#=====
on copyThis:POSIXPath intoFolder:theFolderURL withName:destName
local |⌘|, theSourceURL, theDestURL
set |⌘| to current application
set theSourceURL to |⌘|'s |NSURL|'s fileURLWithPath:POSIXPath
set theDestURL to theFolderURL's URLByAppendingPathComponent:destName
set theFileManager to |⌘|'s NSFileManager's |defaultManager|()
set {theResult, theError} to theFileManager's createDirectoryAtURL:theFolderURL withIntermediateDirectories:true attributes:(missing value) |error|:(reference)
--set {theResult, theError} to theFileManager's createDirectoryAtURL:destURL withIntermediateDirectories:true attributes:(missing value) |error|:(reference)
my copyFromURL:theSourceURL toURL:theDestURL withReplacing:true
end copyThis:intoFolder:withName:
#=====
-- This handler is called by other handlers, and is not meant to called directly
on copyFromURL:sourceURL toURL:destinationURL withReplacing:replaceFlag
set |⌘| to current application
set theFileManager to |⌘|'s NSFileManager's |defaultManager|()
set {theResult, theError} to (theFileManager's copyItemAtURL:sourceURL toURL:destinationURL |error|:(reference))
if not theResult as boolean then
if replaceFlag and (theError's code() as integer = |⌘|'s NSFileWriteFileExistsError as integer) then -- it already exists, so try replacing
-- create suitable temporary directory in destinationURL's parent folder
set {tempFolderURL, theError} to theFileManager's URLForDirectory:(|⌘|'s NSItemReplacementDirectory) inDomain:(|⌘|'s NSUserDomainMask) appropriateForURL:(destinationURL's |URLByDeletingLastPathComponent|()) create:true |error|:(reference)
if tempFolderURL = missing value then error (theError's |localizedDescription|() as text) -- failed, so return error
-- copy sourceURL to temp directory
set tempDestURL to tempFolderURL's URLByAppendingPathComponent:(destinationURL's |lastPathComponent|())
set {theResult, theError} to theFileManager's copyItemAtURL:sourceURL toURL:tempDestURL |error|:(reference)
if not theResult as boolean then
-- copy failed, so delete temporary directory and return error
theFileManager's removeItemAtURL:tempFolderURL |error|:(missing value)
error (theError's |localizedDescription|() as text)
end if
-- replace existing file with temp file atomically, then delete temp directory
set {theResult, theError} to theFileManager's replaceItemAtURL:destinationURL withItemAtURL:tempDestURL backupItemName:(missing value) options:(|⌘|'s NSFileManagerItemReplacementUsingNewMetadataOnly) resultingItemURL:(missing value) |error|:(reference)
theFileManager's removeItemAtURL:tempFolderURL |error|:(missing value)
-- if replacement failed, return error
if not theResult as boolean then error (theError's |localizedDescription|() as text)
else -- replaceFlag is false or an error other than file already exists, so return error
error (theError's |localizedDescription|() as text)
end if
end if
end copyFromURL:toURL:withReplacing:
#=====#=====#=====#=====#=====#=====
on movePath:posixSource toFolder:destURL
local |⌘|, theSourceURL, destURL, shortURL, origName, theFileManager, theResult, theError, destName
set |⌘| to current application
set theSourceURL to |⌘|'s |NSURL|'s fileURLWithPath:posixSource
set theFileManager to |⌘|'s NSFileManager's |defaultManager|()
set {theResult, theError} to theFileManager's createDirectoryAtURL:destURL withIntermediateDirectories:true attributes:(missing value) |error|:(reference)
--if not (theResult as boolean) then error (theError's |localizedDescription|() as text)
# maintenant, move cheminPosixDuFichierSource item
set destName to theSourceURL's |lastPathComponent|()
set destURL to destURL's URLByAppendingPathComponent:destName
my moveFromURL:theSourceURL toURL:destURL withReplacing:true
--return destURL's |path| as text
end movePath:toFolder:
#=====
-- This handler is called by other handlers, and is not meant to called directly
on moveFromURL:sourceURL toURL:destinationURL withReplacing:replaceFlag
set |⌘| to current application
set theFileManager to |⌘|'s NSFileManager's |defaultManager|()
set {theResult, theError} to (theFileManager's moveItemAtURL:sourceURL toURL:destinationURL |error|:(reference))
if not theResult as boolean then
if replaceFlag and (theError's code() = |⌘|'s NSFileWriteFileExistsError) then -- it already exists, so try replacing
-- replace existing file with temp file atomically, then delete temp directory
set {theResult, theError} to theFileManager's replaceItemAtURL:destinationURL withItemAtURL:sourceURL backupItemName:(missing value) options:(|⌘|'s NSFileManagerItemReplacementUsingNewMetadataOnly) resultingItemURL:(missing value) |error|:(reference)
-- if replacement failed, return error
if not theResult as boolean then error (theError's |localizedDescription|() as text)
else -- replaceFlag is false or an error other than file already exists, so return error
error (theError's |localizedDescription|() as text)
end if
end if
end moveFromURL:toURL:withReplacing:
#=====#=====#=====#=====#=====#=====
on buildFullPath:proposedName inFolder:theFolderURL
local |⌘|, proposedName, theDestURL
set |⌘| to current application
if class of proposedName is text then set proposedName to |⌘|'s NSString's stringWithString:proposedName
set proposedName to proposedName's stringByReplacingOccurrencesOfString:"/" withString:":"
set theDestURL to theFolderURL's URLByAppendingPathComponent:proposedName
return theDestURL
end buildFullPath:inFolder:
#=====#=====#=====#=====#=====#=====
on getNameAndContainer:POSIXPath
local theURL, POSIXContainer, PosixName
set theURL to current application's |NSURL|'s fileURLWithPath:POSIXPath
set POSIXContainer to theURL's URLByDeletingLastPathComponent()
set PosixName to (theURL's lastPathComponent()) as text
# ATTENTION, Sierra ne met pas le / final
return {PosixName, POSIXContainer}
end getNameAndContainer:
#=====#=====#=====#=====#=====#=====
Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) vendredi 17 mars 2017 17:23:48
Here I created a folder named “01_Camera” on the desktop (in fact the folder name is never used) then I attached the script.
I dropped the file testFile copie.txt onto the folder icon and the folders 02_Media and 03_Backup where created with the file Film.cine in 02_Media and the file testFile copie.txt in 03_Backup.
I see no instruction able to kill the script in 10.10 (Yosemite).
Disable the folder action
Open the script
Edit the main handler as :
--on adding folder items to this_folder after receiving these_items # DISABLED
set this_folder to (path to desktop as text) & "01_Camera" # ADDED
set these_items to {this_folder & "a.file"} # adjust the name to the file available # ADDED
set FilmCine to "Film.cine"
set name2 to "02_Media"
set name3 to "03_Backup"
set thePOSIXPath to POSIX path of these_items's item 1
# Get the URL pointing to the folder containing this_folder
set {name1, URLContainer} to my getNameAndContainer:(POSIX path of this_folder)
# Build the URL pointing to the folder "02_Media"
set URL2 to my buildFullPath:name2 inFolder:URLContainer
# Build the URL pointing to the folder "03_Backup"
set URL3 to my buildFullPath:name3 inFolder:URLContainer
# Copy in "02_Media2" as "Film.cine"
my copyThis:thePOSIXPath intoFolder:URL2 withName:FilmCine
# Move to "03_Backup"
my movePath:thePOSIXPath toFolder:URL3
--end adding folder items to # DISABLED
execute the script.
You will see if it works or at which instruction it fails.
I will look again in Xcode help to check if I missed a feature requiring 10.11.
Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) samedi 18 mars 2017 10:45:54
on adding folder items to this_folder after receiving these_items
--set these_items to {((path to desktop as text) & "01_kamera:testFile copie.txt") as alias}
--tell application "Finder" to set this_folder to container of these_items's item 1
set FilmCine to "Film.cine"
set name2 to "02_Media"
set name3 to "03_Backup"
tell application "Finder"
# Extracts the path of container of this_folder
set theContainer to (container of this_folder) as text
# Extracts the dropped file
set theFile to these_items's item 1
# Builds the path to folder 02_Media
set pathToFolder2 to theContainer & name2
# Creates folder 02_Media if it doesn't exist
if not (exists folder pathToFolder2) then make new folder at folder theContainer with properties {name:name2}
# Builds the path to folder 03_Backup
set pathToFolder3 to theContainer & name3
# Creates folder 03_Backup if it doesn't exist
if not (exists folder pathToFolder3) then make new folder at folder theContainer with properties {name:name3}
# Builds the path to the file Film.cine
set path2FilmCine to pathToFolder2 & ":" & FilmCine
# If the file already exists, delete it
if exists item path2FilmCine then delete item path2FilmCine
duplicate theFile to folder pathToFolder2 # returns a reference to the copied file
# Rename the copied file
set name of result to FilmCine
move theFile to folder pathToFolder3
end tell
end adding folder items to
Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) samedi 18 mars 2017 11:33:09
error “|NSURL| doesn’t understand the “fileURLWithPath_” message.” number -1708 from NSURL
(it was referring to the last part of the script)
#=====#=====#=====#=====#=====#=====
on getNameAndContainer:POSIXPath
local theURL, POSIXContainer, PosixName
set theURL to current application's |NSURL|'s fileURLWithPath:POSIXPath
set POSIXContainer to theURL's URLByDeletingLastPathComponent()
set PosixName to (theURL's lastPathComponent()) as text
# ATTENTION, Sierra ne met pas le / final
return {PosixName, POSIXContainer}
I’m quite sure that you dropped at least one instruction at the beginning of the original script.
use AppleScript version "2.4" # requires at least Yosemite
use scripting additions
use framework "Foundation" # this one
I may replicate what you describe if I disable the instruction use framework “Foundation”
Did you used the button [Open this Scriplet in your Editor] or used copy/paste to get the script in your editor?
Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) samedi 18 mars 2017 16:38:03