Copie Applescript

Maxmad68

Membre actif
27 Octobre 2014
313
17
Strasbourg
github.com
Salut
Encore une petite question, comment puis-je faire pour faire une copie en Applescript (j'adore ce langage, ça s'est vu?:cat:) sans afficher cette petite fenêtre:
3rfy.tiff

Lien pour la photo si elle ne s'affiche pas
Je voudrais utiliser, si possible des alias Applescript ("Ordi:Users:moi:dossier:fichier.txt") et non des chemins terminals ("~/dossier/fichier.txt")
Sinon, comment pourrais-je faire pour convertir les chemins Applescript en chemins terminals?

Merci
 
Bonjour,


Une solution serait d'utiliser NSfileManager en AppleScriptObjc :

Voici un exemple de code qui copie un élément dans un autre dossier:
AppleScript:
set thisItem to POSIX path of source
set dest to POSIX path of destFolder

set tname to thisItem's lastPathComponent -- récupère le nom  de l'élément à copier
set d to dest's stringByAppendingPathComponent:tname -- ceci ajoute le nom de l'élément à copier au dossier de destination (obligatoire)
set fm to current application's NSFileManager's defaultManager()
set x to fm's copyItemAtPath:thisItem toPath:d |error|:(missing value) -- copie
if x then
    display alert "Copie terminée"
else
    display alert "Aucune copie"
end if

La variable source contient le chemin de l'élément à copier (de type alias ou HFS)
La variable destFolder contient le chemin du dossier de destination (de type alias ou HFS)
Info, HFS est un chemin de ce type "Ordi:Users:moi:dossier:fichier.txt"
 
Bonjour, pour s'amuser et sans avoir recours à divers extensions.

Bloc de code:
on func_posix_shell_exec(cmd)
    set result to do shell script cmd
    return result
end func_posix_shell_exec

on func_posix_dirname(apath)
    set cmd to "dirname" & space & quoted form of apath
    return func_posix_shell_exec(cmd)
end func_posix_dirname

on func_posix_basename(apath)
    set cmd to "basename" & space & quoted form of apath
    return func_posix_shell_exec(cmd)
end func_posix_basename

on func_posix_is_kind_opt(apath, flags)
    set cmd to "eval 'if [ " & flags & space & quoted form of apath & " ]; then echo 1; else echo 0; fi' 2>&1"
    return func_posix_shell_exec(cmd)
end func_posix_is_file

on func_posix_is_file(apath)
    return func_posix_is_kind_opt(apath, "-f")
end func_posix_is_file

on func_posix_is_dir(apath)
    return func_posix_is_kind_opt(apath, "-d")
end func_posix_is_dir

on func_posix_create_dir(dirpath)
    set cmd to "/bin/mkdir -p " & quoted form of dirpath & " 2>&1"
    return func_posix_shell_exec(cmd)
end func_posix_create_dir

on func_posix_create_rmdir_opt(dirpath, flags)
    set cmd to "/bin/rmdir" & space & flags & space & quoted form of dirpath & " 2>&1"
    return func_posix_shell_exec(cmd)
end func_posix_create_rmdir_opt

on func_posix_create_rmdir(dirpath)
    return func_posix_create_rmdir_opt(dirpath, "-f")
end func_posix_create_rmdir

on func_posix_create_rmdir_r(dirpath)
    return func_posix_create_rmdir_opt(dirpath, "-Rf")
end func_posix_create_rmdir_r

on func_posix_create_rmfile_opt(filepath, flags)
    set cmd to "/bin/rm" & space & flags & space & quoted form of filepath & " 2>&1"
    return func_posix_shell_exec(cmd)
end func_posix_create_rmfile_opt

on func_posix_create_rmfile(filepath)
    return func_posix_create_rmdir_opt(filepath, "-f")
end func_posix_create_rmfile

on func_posix_copy_dir_opt(src_dirpath, dest_dirpath, flags)
    set cmd to "/usr/bin/ditto" & space & flags & space & quoted form of src_dirpath & space & quoted form of dest_dirpath
    return func_posix_shell_exec(cmd)
end func_posix_copy_dir_opt

on func_posix_copy_file_opt(src_filepath, dest_filepath, flags)
    set cmd to "/bin/cp" & space & flags & space & quoted form of src_filepath & space & quoted form of dest_filepath
    return func_posix_shell_exec(cmd)
end func_posix_copy_file_opt

on func_posix_copy_dir(src_dirpath, dest_dirpath, flags)
    return func_posix_copy_dir_opt(src_dirpath, dest_dirpath, "-rsrc")
end func_posix_copy_dir

on func_posix_copy_file(src_filepath, dest_dirpath)
    set bname to func_posix_basename(src_filepath)
    set dest to dest_dirpath & "/" & bname
    return func_posix_copy_file_opt(src_filepath, dest, "-f")
end func_posix_copy_file

--

set p to POSIX path of (path to application "Finder")
set posix_dirname to func_posix_dirname(p)
set posix_basename to func_posix_basename(p)

-- EOF
 
Dernière édition: