In this example, if you swap the commented status of the 2 last lines, the duplicated file (indyScript.scpt) is in place.
Otherwise, it vanishes!
Is there something I’m missing?
use framework "Foundation"
use framework "AppKit"
use scripting additions
tell application id "asDB"
activate
-- récupère le document source
set currentDoc to front document
-- vérifie que le document compile
if not (compile currentDoc) then display alert "Impossible de compller le script en cours." message "Veuillez corriger votre code." buttons {"Annuler"} cancel button 1
-- récupère le chemin au document source
set fileName to name of currentDoc
set fileSpec to file spec of currentDoc
if fileSpec = missing value then display alert "Le script actif n'est pas enregistré." message "Impossible de dupliquer un document sans chemin d'accès." buttons {"Annuler"} cancel button 1
set sourceURL to current application's NSURL's fileURLWithPath:(POSIX path of fileSpec)
set destURL to sourceURL's URLByDeletingPathExtension()'s URLByAppendingPathExtension:"scptd"
set destHFS to destURL as «class furl»
-- enregistre en bundle
save currentDoc in destHFS as bundled compiled script
-- definit les URLs
set theMain to destURL's URLByAppendingPathComponent:"Contents/Resources/Scripts/main.scpt"
set theDup to theMain's URLByDeletingLastPathComponent()'s URLByAppendingPathComponent:"indyScript.scpt"
-- verifie l'existence d'un précédent fichier
set theFileManager to current application's NSFileManager's defaultManager()
if theDup's checkResourceIsReachableAndReturnError:(missing value) then
display alert "Le fichier «indySript» existe déjà." message "Poursuivre l'enregistrement aurait pour effet de perdre le code effectif du script." buttons {"Annuler"} cancel button 1
end if
-- duplique et renomme le script principal
set theResult to theFileManager's copyItemAtURL:theMain toURL:theDup |error|:(missing value)
if theResult = false then display alert "alertText" message "alertMess" buttons {"Annuler"} cancel button 1
-- modifie le script main du bundle
set source text of front document to "
set theName to my name
set theScript to \"\" & (path to me) & \"Contents:Resources:Scripts:indyScript.scpt\"
set theName to text ((offset of \")\" in theName) + 1) thru -1 of theName
tell application id \"InDn\"
set enable redraw of script preferences to false
do script theScript undo name theName undo mode fast entire script
set enable redraw of script preferences to true
end tell
"
-- enregistre le document original
(display alert "Veuillez confirmer l'enregistrement de «" & fileName & "»." message "Deux précautions valent mieux qu'une !" buttons {"Annuler", "OK"} default button 2 cancel button 1)
tell application "System Events" to keystroke "s" using command down
--save front document
end tell