Je vous propose ici un programme qui vous permettra, en un clic, de créer un nouveau fichier dans le dossier actif. Il utilisera, si défini, un modèle de fichier en fonction de l’extension donnée.
C’est un script qui permet de créer rapidement un fichier sous Mac OSX en utilisant un modèle !
(Finder Toolbar Script) – Contient le programme Applescript et le dossier contenant des modèles.
Principe de base du Script «Quickfile»
- Clic sur le bouton de la barre d’outil par l’utilisateur
- Détecte le dossier où se trouve l’utilisateur et demande le nom du fichier.
- Vérification que le fichier n’existe pas (si oui il redemande un nouveau nom).
- Si une extension est ajoutée au nom (exemple : .php)
- il vérifie la présence du dossier de template à l’endroit où se trouve le script.
- Si le dossier «QuickFileModels» existe et contient un fichier template (sous la forme <extension>.txt)
- il récupère le contenu du fichier
- Le fichier est crée à l’endroit où vous vous trouvez.
- Création du fichier vide ou depuis le template.
Installation
- Copier le fichier du script QuickFile.app et le dossier QuickFileModels dans le dossier de script de l’utilisateur (exemple : <utilisateur>/Library/Scripts/Finder Toolbar/)
- Glisser le fichier QuickFile.app dans la barre d’outils d’une fenêtre du Finder.
- Éditer vos modèles dans le dossier QuickFileModels (nom : <extension>.txt)
- Vous pouvez tester en ouvrant un dossier et en cliquant sur le bouton pour créer rapidement le nouveau fichier.
tell application "Finder" -- Le dossier utilisé set thisFolder to (target of front window) as Unicode text -- Choix du nom du fichier set thefilename to my ChooseName(thisFolder) -- Le chemin complet set theFilePath to thisFolder & thefilename -- Création du fichier set newFile to make new file with properties {name:thefilename} at thisFolder as alias -- verification de l'extension fournie et ajout de texte si true set theExtension to my extensionFilter(thefilename) if (theExtension is not "") then set templateContent to my CheckTemplate(theExtension) -- SI LE TEMPLATE EXISTE (CONTENU TEXTE) ON ENREGISTRE LE TEXTE if (templateContent is not "") then try set fileID to open for access file theFilePath with write permission write templateContent to fileID as string close access fileID on error try close access fileID end try display dialog "Le Template n'a pas pu être enregistré." end try end if end if end tell on ChooseName(thisFolder) -- DEMANDE LE NOM DU FICHIER & VERIFIE SIL EXISTE PAS tell application "Finder" set thefilename to text returned of (display dialog "NOM DU FICHIER :" & return & "(Un template sera choisie en fonction de l'extension)" default answer "newFile.txt") set theFilePath to thisFolder & thefilename -- Verification que le fichier n'existe pas if exists file theFilePath then display alert "Le fichier ‘" & thefilename & "’ existe déjà." as informational set thefilename to my ChooseName(thisFolder) end if end tell return thefilename end ChooseName on extensionFilter(thefilename) -- RECUPERE EXTENSION CHOISIE set theExtension to thefilename as string set theExtension to (the reverse of every character of theExtension) as string -- inversion du nom set x to the offset of "." in theExtension -- on recupere la position du point de l'extension if (x < 6 and x is not 0) then set theExtension to (text 1 thru (x - 1) of theExtension) as string set theExtension to (the reverse of every character of theExtension) as string else -- MISE A ZERO set theExtension to "" end if return theExtension end extensionFilter on CheckTemplate(theExtension) -- VERIFIE SI UN TEMPLATE EXISTE ET LE RENVOI SI OUI set myPath to (path to me) --chemin du script tell application "Finder" set templateFolder to folder of myPath as alias set templateFolder to folder "QuickFileModels" of templateFolder as alias set theTemplate to templateFolder & theExtension & ".txt" as string if exists file theTemplate then try -- LE TEMPLATE EXISTE ON ON PEUX RECUPERER LE TEXTE set the theTemplate to the theTemplate as string set fileID to open for access theTemplate --with write permission set lecontenu to read fileID close access fileID on error try close access fileID end try return false end try else -- SI LE TEMPLATE N'EXISTE PAS RETOURNE UN CONTENU VIDE set lecontenu to "" end if end tell return lecontenu end CheckTemplate
Quick File is an simple applescript program designed to quickly create new file from the finder toolbar and directly in the folder you are. I made this script specially to share it with other user of Mac OSX.
Install this application and the template folder in your script directory, and drag the application in the finder toolbar. Now you can start to create file based on template situated in the folder « QuickFileModels ».
How it’s work ? Tell a new name for the file, verify that the file doesn’t exist, and check if an template exist with the same extension (work with extension like .txt / .js / .as / .php and… .psd).
This application is distributed as it. I am not responsible for any bad use of this program (I’m not a developer). If you think of functionality to give this script better or to report any bug, don’t hesitate to tell me.
![Agonia Studios [Blog]](http://www.agonia-studios.fr/blog/images/logo-weblog.png)











