passer un script run en "on adding folder items"

papouuu

Membre enregistré
17 Septembre 2009
2
0
34
bonjour a tous alors voila je vous expose mon problème vu que je débute sur applescript et sur mac en général. j'ai un script qui me renomme mes fichiers avec la date de création, comment le modifier pour que ce script s'execute automatiquement sans avoir besoin de le lancer?

voila le script:

on run
-- get the list of selected ID's in front window
set selectedItems to GetSelection()

-- show about
AboutScript()

-- process each item
set theCount to 0
tell application "ExtendScript Toolkit 2"
repeat with theItem in selectedItems
-- get exif date & original name
set newName to ""
set theName to the name of theItem
set thePath to the «class pPat» of theItem
--display dialog thePath
set theDate to the «class pE0e» of theItem

--try
-- form new name
set theYear to ((year of theDate) as string)
set theMonth to my getPaddedString(my getMonthNumber(theDate))
set theDay to my getPaddedString(day of theDate)
set theHours to my getPaddedString((time of theDate) div hours)
set theMins to my getPaddedString(((time of theDate) mod hours) div minutes)
set theSecs to my getPaddedString((time of theDate) mod minutes)
set newName to theYear & "-" & theMonth & "-" & theDay & "-" & theHours & "-" & theMins & "-" & theSecs

-- if the old name has an extension, append it to new name
set nc to the number of characters in theName
if character (nc - 3) of theName = "." then
set thefileextension to (get text (nc - 3) through nc of theName)
end if

-- rename
if theName ≠ (newName & thefileextension) then

--does a file already exist with this name?
tell application "Finder"
set filefolder to (folder of alias thePath)
set filenumber to ""
set counter to 1
set myloop to true
repeat until myloop is false
if exists file (newName & filenumber & thefileextension) of filefolder then
if filenumber = "" then
set filenumber to "_" & counter
else
set filenumber to "_" & counter + 1
set counter to counter + 1
end if
else
set myloop to false
end if
end repeat
end tell
set the name of theItem to (newName & filenumber & thefileextension)
if the name of theItem = (newName & filenumber & thefileextension) then set theCount to theCount + 1
end if
--end try
end repeat
end tell

ShowResult(theCount)
end run


-- get the selected media items in an array ---------------------------------------------
on GetSelection()
set selectedItems to {}
tell application "ExtendScript Toolkit 2"
if window 1 exists then set selectedItems to the selection of window 1
end tell
if number of items in selectedItems is 0 then
display dialog ¬
"You need to select at least one media item in the front catalog in order to use this script." buttons {"OK"} default button ¬
"OK" with icon 1 giving up after 10
error number -128
end if
return selectedItems
end GetSelection


-- about this script ---------------------------------------------

on AboutScript()
display dialog ¬
"This script will rename original files of all selected items using valid values in the \"Capture Date\" field." buttons {"Cancel", "Rename"} default button 2 with icon 1

set theAnswer to the button returned of the result
return theAnswer
end AboutScript


-- show result ---------------------------------------------

on ShowResult(theCount)
if theCount = 0 then
set theMsg to "Script completed." & return & "No items were renamed."
else if theCount = 1 then
set theMsg to "Script completed." & return & "1 item was renamed."
else
set theMsg to "Script completed." & return & theCount & " items were renamed."
end if
display dialog theMsg buttons {"OK"} default button "OK" with icon 1 giving up after 10
end ShowResult



on getPaddedString(aNumber)
if aNumber < 10 then
return "0" & (aNumber as string)
else
return (aNumber as string)
end if
end getPaddedString

on getMonthNumber(aDate)
if month of aDate is January then
return 1
else if month of aDate is February then
return 2
else if month of aDate is March then
return 3
else if month of aDate is April then
return 4
else if month of aDate is May then
return 5
else if month of aDate is June then
return 6
else if month of aDate is July then
return 7
else if month of aDate is August then
return 8
else if month of aDate is September then
return 9
else if month of aDate is October then
return 10
else if month of aDate is November then
return 11
else if month of aDate is December then
return 12
end if
end getMonthNumber
 

papouuu

Membre enregistré
17 Septembre 2009
2
0
34
changement de question: j'ai réussi a bidouiller avec de l'aide pour avoir ce script:

on adding folder items to CeDossier after receiving theseItems

AboutScript()


tell application "/System/Library/CoreServices/Folder Actions Dispatcher.app" to set polling interval to 1
--tell application "AppleScript Editor"
repeat with anItem in theseItems
set {theName, theExt, creationDate} to {name, name extension, creation date} of (info for anItem without size)
if theExt is not missing value then
set theName to text 1 thru -((length of theExt) + 2) of theName
set theExt to "." & theExt
else
set theExt to ""
end if
set {t_date, t_Time, theYear} to {short date string, time string, year} of creationDate
tell t_date to set t_date to ("" & (theYear as string) & "-" & (text 4 thru 5) & "-" & (text 1 thru 2)) --
tell t_Time to set t_Time to (text 1 thru 2) & "h" & (text 4 thru 5) & "m" & (text 7 thru 8)

set start_path to (CeDossier as string) & t_date & "-" & t_Time & "_" & theName

try -- **** ceci vérifie que le nouveau nom n'existe pas dans le dossier ****
set i to ""
(start_path & i & theExt) as alias
set start_path to start_path & " " -- le séparateur est une espace
repeat with i from 1 to 1000 -- le nom existe, on ajoute une espace et un nombre à la fin du nom
(start_path & i & theExt) as alias
end repeat
end try ---**** fin de la vérification du nom dans le dossier ****


do shell script "/bin/mv " & (quoted form of POSIX path of anItem) & " " & (quoted form of POSIX path of (start_path & i & theExt))
end repeat
--end tell
end adding folder items to

on AboutScript()
display dialog ¬
"This script will rename original files of all selected items using valid values in the \"Capture Date\" field." buttons {"Cancel", "Rename"} default button 2 with icon 1

set theAnswer to the button returned of the result
return theAnswer
end AboutScript

mais je voudrais récupérer la date du cliché, comment peut-on faire?
 
Salut,

Il est inutile de mettre la ligne
Bloc de code:
tell application "/System/Library/CoreServices/Folder Actions Dispatcher.app" to set polling interval to 1
dans ton script, il suffit d'exécuter cette ligne 1 seule fois pour que le polling interval reste à 1… ;)