Problèmes avec Automator

Serge 001

Membre actif
3 Juillet 2004
148
2
Montréal
Bonjour à tous,
Deux questions à propos d'un problème de fonctionnement d'un processus Automator

Ce processus est censé obtenir le contenu du document TextEdit qui se trouve l'avant-plan, le copier dans le Presse-papiers et finalement le transférer à l'aide d'un AppleScript dans le dossier Notes de l'iPod.
Donc, la première action est Obtenir le contenu du document TextEdit.
La suivante, Copier dans le Presse-papiers.
La dernière est Exécuter un script AppleScript dans laquelle j'insère le script (Clipboard to Notes) suivant :

property section_length : 3950

try
display dialog "Create Note from Clipboard" & return & return & "This scirpt will create a new iPod note from the contents of the clipboard." buttons {"Cancel", "Continue"} default button 2

set this_data to the clipboard as string

-- check for iPods
set the mounted_iPods to my locate_iPods()
-- check for iPod count
if the mounted_iPods is {} then
error "No iPod is connected to this computer."
else if the (count of the mounted_iPods) is greater than 1 then
-- choose iPod
set the ipod_names to {}
repeat with i from 1 to the count of the mounted_iPods
set this_iPod to item i of the mounted_iPods
tell application "Finder"
set the end of the ipod_names to the name of this_iPod
end tell
end repeat
set this_name to (choose from list ipod_names with prompt "Pick the iPod to use:") as string
if this_name is "false" then error number -128
repeat with i from 1 to the count of the ipod_names
if item i of the ipod_names is this_name then
set this_iPod to item i of the mounted_iPods
exit repeat
end if
end repeat
else
set this_iPod to item 1 of the mounted_iPods
end if

tell application "Finder"
-- check for Notes folder
if not (exists folder "Notes" of this_iPod) then
make new folder at this_iPod with properties {name:"Notes"}
end if
set the notes_folder to (folder "Notes" of this_iPod) as alias
set the subfolder_count to the count of (get folders of the notes_folder)
end tell

if the subfolder_count is greater than 0 then
-- get a list of the sub-folders in POSIX format of the entire contents of the target folder
copy my POSIX_folders(notes_folder, true) to {source_path, folder_paths}
-- prompt the user to pick a destination folder
set this_folder to (choose from list folder_paths with prompt "Pick the destination folder:") as string
if this_folder is "false" then error number -128
-- create an alias reference based on the user choice
set the notes_folder to (source_path & this_folder) as POSIX file as alias
end if

-- get a list of the existing note names
set the existing_notes to list folder notes_folder without invisibles
set this_name to ""
repeat
try
display dialog "Enter a name for this note:" default answer this_name
set this_name to the text returned of the result
if this_name is in the existing_notes then
error "A note named ?" & this_name & "? already exists." & return & return & "Enter another name."
else if this_name is not "" then
exit repeat
end if
on error the error_message number the error_number
if the error_number is not -128 then
display dialog error_message
else
error number -128
end if
end try
end repeat

set the data_length to the length of this_data
if the data_length is less than 4000 then
set the target_file to (notes_folder as string) & this_name
set the write_result to my write_to_file(this_data, target_file, false)
if the write_result is true then
display dialog "The note ?" & this_name & "? has been created." buttons {"?"} default button 1 giving up after 2
else
error "There was a problem writing the note to the iPod."
end if
else
set the file_count to round (data_length / section_length) rounding up
repeat with i from 1 to the file_count
set the file_number to my add_leading_zeros(i, 1)
set the next_number to my add_leading_zeros((i + 1), 1)
set the filename to this_name & "." & file_number
set the nextname to this_name & "." & next_number
if i is 1 then -- first list item
set the section_text to text from character 1 to section_length of this_data & "..." & return & return & tab & "[[<a href=\"" & nextname & "\">NEXT PAGE</a>]]"
set the target_file to (notes_folder as string) & this_name
else if i is the file_count then -- last list item
set the section_text to "[[PAGE " & (i as string) & " of " & (file_count as string) & "]]" & return & return & "..." & (text from character (section_length * (i - 1)) to -1 of this_data)
set the target_file to (notes_folder as string) & filename
else -- other list items
set the section_text to "[[PAGE " & (i as string) & " of " & (file_count as string) & "]]" & return & return & "..." & (text from character (section_length * (i - 1)) to (section_length * i) of this_data) & "..." & return & return & tab & "[[<a href=\"" & nextname & "\">NEXT PAGE</a>]]"
set the target_file to (notes_folder as string) & filename
end if
--set the target_file to (notes_folder as string) & filename
set the write_result to my write_to_file(section_text, target_file, false)
if the write_result is false then error "There was a problem writing note ?" & filename & "? to the iPod."
end repeat
display dialog "The note ?" & this_name & "? has been created." buttons {"?"} default button 1 giving up after 2
end if
on error error_message number the error_number
if the error_number is not -128 then
display dialog error_message buttons {"OK"} default button 1
end if
end try

on POSIX_folders(source_folder, include_source)
if include_source is true then
set path_offset to 4
set the source_path to POSIX path of the source_folder
set AppleScript's text item delimiters to "/"
set the source_foldername to text item -2 of the source_path
set the source_path to ((text items 1 thru -3 of the source_path) as string) & "/"
set AppleScript's text item delimiters to ""
set the folder_paths to {(source_foldername & "/")}
else
set path_offset to 5
set the source_path to POSIX path of the source_folder
set the folder_paths to {}
end if
try
tell application "Finder"
try
set these_folders to every folder of the entire contents of the source_folder as alias list
on error
set these_folders to every folder of the entire contents of the source_folder as alias as list
end try
end tell
-- convert alias references to POSIX paths
set AppleScript's text item delimiters to "/"
repeat with i from 1 to the count of these_folders
set this_folder to item i of these_folders
set this_path to POSIX path of this_folder
-- extract the POSIX path starting at the source folder
set this_path to (text items path_offset thru -1 of this_path) as string
set the end of the folder_paths to this_path
end repeat
set AppleScript's text item delimiters to ""
return {source_path, folder_paths}
on error
return {source_path, false}
end try
end POSIX_folders

on locate_iPods()
set the volumes_directory to "/Volumes/" as POSIX file as alias
set the volume_names to list folder volumes_directory without invisibles
set mounted_iPods to {}
repeat with i from 1 to the count of volume_names
try
set this_name to item i of volume_names
set this_disk to ("/Volumes/" & this_name & "/") as POSIX file as alias
set these_items to list folder this_disk
if "iPod_Control" is in these_items then
set the end of the mounted_iPods to this_disk
end if
end try
end repeat
return mounted_iPods
end locate_iPods

on write_to_file(this_data, target_file, append_data)
try
set the target_file to the target_file as text
set the open_target_file to ¬
open for access file target_file with write permission
if append_data is false then ¬
set eof of the open_target_file to 0
write this_data to the open_target_file starting at eof
close access the open_target_file
return true
on error
try
close access file target_file
end try
return false
end try
end write_to_file

on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars

on add_leading_zeros(this_number, max_leading_zeros)
set the threshold_number to (10 ^ max_leading_zeros) as integer
if this_number is less than the threshold_number then
set the leading_zeros to ""
set the digit_count to the length of ((this_number div 1) as string)
set the character_count to (max_leading_zeros + 1) - digit_count
repeat character_count times
set the leading_zeros to (the leading_zeros & "0") as string
end repeat
return (leading_zeros & (this_number as text)) as string
else
return this_number as text
end if
end add_leading_zeros


J'appuie sur exécuter... et rien ne se produit.
J'enlève de cette action les caractères ( * *) qui encadrent le script et ça ne marche pas plus.

En désespoir de cause, j'enregistre l'AppleScript comme application
et je substitue la troisième action du processus (exécuter le script AppleScript) par Lancer l'application. J'appuie sur exécuter et tout se passe à merveille. Sauf que lorsque je sauvegarde le processus (sous forme d'application) et que je clique dessus, il n'active que les deux premières actions et s'arrête là, tout simplement. Je relance à nouveau le
processus à partir d'Automator et ça fonctionne sans pépin une seconde fois.
Questions : pourquoi Automator refuse-t-il d'exécuter l'AppleScript dans la première version du processus et pourquoi le processus, dans sa deuxième version, ne s'exécute jusqu'à la fin que lorsqu'il est lancé à partir d'Automator ?
J'espère que mes explications sont claires, car moi-même j'en perds mon latin