L'idée est ici de transformer tout utilisateur en "power user" utilisant des scripts pour automatiser certaines tâches. Bien sûr, chatGPT écrit les scripts pour vous. Voici quelques exemples.
Les prompts sont écrits en anglais mais cela doit pouvoir se faire en français sans problème.
Les quatre exemples ci-dessous sont :
1. Obtenir une liste des PDFs et d'obtenir le liens au format Markdown
2. Choisir un fichier et de le dupliquer autant de fois que désiré
3. Choisir un dossier et supprimer tous les fichiers .dmg
4. Renommer tous les fichiers d'un dossier en une seule fois
# Get PDF Files With Markdown Formatting
## Prompt
Give me an apple script providing a list of pdf files in a specific folder the script asks me to choose. Use the POSIX path for the file links and make sure that each link has the full path (i.e. "file:///Users/..."). Then I would need to export this list which must be a bulleted list built with dashes. Create a new line for each item.
[Link to chat](https://chat.openai.com/share/28dbf30d-9fe6-4d6a-8e88-6f4d3102f7c2)
## Code
```AppleScript
-- Prompt the user to choose a folder
set chosenFolder to choose folder with prompt "Select a folder containing PDF files"
set folderPath to POSIX path of chosenFolder
-- Create a list of PDF files in the chosen folder
set pdfFiles to paragraphs of (do shell script "find " & quoted form of folderPath & " -name '*.pdf' -type f")
-- Check if there are any PDF files in the folder
if (count pdfFiles) = 0 then
display dialog "No PDF files found in the selected folder."
else
-- Create a plain text list with links in Markdown format
set textList to ""
repeat with aFile in pdfFiles
set pdfName to name of (info for aFile)
set posixPath to POSIX path of aFile
set textList to textList & " - [" & pdfName & "](" & "file://" & my fixPath(posixPath) & ")" & linefeed
end repeat
-- Save the plain text list to a text file
set listPath to (choose file name default name "PDF_File_List.txt" default location chosenFolder)
set listFile to open for access listPath with write permission
write textList to listFile
close access listFile
-- Display a confirmation
display dialog "List of PDF files in plain text format with Markdown links has been saved to " & listPath
end if
-- Handler to remove double slashes from the file path
on fixPath(filePath)
set {TID, text item delimiters} to {text item delimiters, "//"}
set pathItems to text items of filePath
set text item delimiters to "/"
set filePath to pathItems as text
set text item delimiters to TID
return filePath
end fixPath
```
# Duplicate Files
## Prompt
Write an apple script able to duplicate a file. Use the POSIX file conversion for file paths. It goes without saying that each duplicated file have a unique name. The script should begin by asking the user to select a file and how many time does he or she wants to duplicate it. Then, ask where to save the files. Create a folder containing all duplicated files.
[Link to chat](https://chat.openai.com/share/b6a602e0-2ffc-4f7d-9ff8-3b264ced4e76
## Code
``` AppleScript
-- Prompt the user to select a file
set selectedFile to choose file with prompt "Select a file to duplicate"
-- Prompt the user for the number of duplications
set numberOfDuplicates to text returned of (display dialog "Enter the number of duplications:" default answer "1")
-- Convert the number of duplications to an integer
set numberOfDuplicates to (numberOfDuplicates as integer)
-- Prompt the user to choose a destination folder for duplicated files
set destinationFolder to choose folder with prompt "Select a destination folder for duplicated files"
-- Create a folder for the duplicated files
set currentDate to current date
set folderName to "Duplicated_Files_" & (year of currentDate as string) & ¬
"-" & (month of currentDate as string) & "-" & (day of currentDate as string) & " " & ¬
(time of currentDate as string)
set duplicatedFilesFolder to (destinationFolder as text) & folderName
do shell script "mkdir -p " & quoted form of POSIX path of duplicatedFilesFolder
-- Get the name and extension of the selected file
set {file_name, file_extension} to my getFileInfo(selectedFile)
-- Duplicate the file the specified number of times
tell application "Finder"
activate
repeat with i from 1 to numberOfDuplicates
set duplicatedFileName to file_name & " Copy " & i & file_extension
set duplicatedFile to (duplicatedFilesFolder as text) & duplicatedFileName
duplicate selectedFile to folder duplicatedFilesFolder
set name of result to duplicatedFileName
end repeat
end tell
-- Display a completion message
display dialog "Files duplicated successfully and stored in folder '" & folderName & "'."
-- Open the duplicated files folder in Finder
tell application "Finder"
activate
open folder duplicatedFilesFolder
end tell
on getFileInfo(selectedFile)
set file_name to name of (info for selectedFile)
set AppleScript's text item delimiters to "."
set file_extension to "." & last text item of file_name
set file_name to text 1 thru ((count file_name) - (count file_extension) - 1) of file_name
return {file_name, file_extension}
end getFileInfo
```
# Delete Files
## Prompt
Write an apple script that looks for and deletes dmg files. The script starts by asking the user to select a folder, show the different files that have been found and if the user wish to proceed.
[Link to chat](https://chat.openai.com/share/89321e44-f905-4ca6-8dc3-b8bcea0d84bf)
## Code
``` AppleScript
-- Ask the user to select a folder
set chosenFolder to choose folder with prompt "Select a folder to search for .dmg files:"
if chosenFolder is not equal to "" then
-- Create a list to store found .dmg files
set dmgFiles to {}
-- Get a list of files in the chosen folder
tell application "Finder"
set folderContents to every file of folder chosenFolder whose name extension is "dmg"
end tell
-- Check if any .dmg files were found
if (count of folderContents) is greater than 0 then
-- Display a dialog with the found .dmg files
set fileNames to {}
repeat with dmgFile in folderContents
set end of fileNames to name of dmgFile
end repeat
set fileList to (items of fileNames) as text
set userResponse to display dialog "The following .dmg files were found in the selected folder:" & return & fileList & return & "Do you want to delete them?" buttons {"Cancel", "Delete"} default button "Delete"
if button returned of userResponse is "Delete" then
-- Delete the .dmg files
repeat with dmgFile in folderContents
tell application "Finder"
delete dmgFile
end tell
end repeat
display dialog "The selected .dmg files have been deleted."
else
display dialog "Deletion canceled."
end if
else
display dialog "No .dmg files found in the selected folder."
end if
else
display dialog "No folder selected. Script terminated."
end if
```
# Rename All Files
## Prompt
Write an apple script asking the user to select a folder and to pick a name. Then, the script rename each files with this name and a number (1 for the first file, 2 for the seconde one and so on). Ignore files like ".DS_Store."
[Link to chat](https://chat.openai.com/share/1412e8bf-c44d-4c05-b657-3e5b6b0012ca)
## Code
Impossible d'obtenir un code fonctionnant. chatGPT m'a invité à utiliser Automator à la place.
![[automator.png]]
Voici les différentes étapes du workflow. Il y a quelques erreurs dans la description de chatGPT (mais voir capture d'écran ci-dessus) et il semble qu'on puisse faire plus court.
>1. Open Automator.
>2. Create a new Automator document by choosing "File" > "New" and selecting "Workflow."
>3. In the Library column on the left, find "Files & Folders" and drag the "Ask for Finder Items" action to the workflow area.
>4. In the "Ask for Finder Items" action, set "Prompt" to a message like "Please select a folder."
>5. In the Library column, find "Text" and drag the "Ask for Text" action to the workflow area. This will prompt you to enter a name.
>6. In the "Ask for Text" action, set "Prompt" to something like "Enter a name."
>7. Find and drag the "Set Value of Variable" action to the workflow area.
>8. In the "Set Value of Variable" action, create a new variable by clicking the "Variable" field and selecting "New Variable." Name it something like "Base Name."
>9. Connect the "Ask for Text" action to the "Set Value of Variable" action.
>10. In the Library column, find and drag the "Get Specified Finder Items" action to the workflow area.
>11. Click "Add" in the "Get Specified Finder Items" action, and choose a test file to include in the workflow.
>12. In the Library column, find and drag the "Get Folder Contents" action to the workflow area.
>13. In the "Get Folder Contents" action, set "Repeat for each subfolder found" to "Off."
>14. In the Library column, find and drag the "Rename Finder Items" action to the workflow area.
>15. In the "Rename Finder Items" action, set "Rename" to "Make Sequential."
>16. Set "Name these files" to the "Base Name" variable.
>17. Set "Where" to "Same folder."
>18. Save your workflow with a descriptive name (e.g., "Rename Files").
>
>Now, when you run this workflow, it will:
>
>1. Prompt you to select a folder.
>2. Prompt you to enter a name (the name will be saved as the "Base Name" variable).
>3. Rename the files within the selected folder using the entered base name and appending a unique number to each file.
>
>This setup will ask you for the name each time you run the automation.