Pages

30 juin 2013

POO




Pour prendre une analogie culinaire, l'approche classique en programmation est de rédiger des recettes, autrement dit la description des opérations à exécuter pour obtenir un résultat. Par exemple, la recette de la sauce hollandaise. Ensuite on s'avise qu'elle a beaucoup de points communs avec celle du beurre blanc, on réunit les parties communes en sous-programmes.

La Programmation Orientée Objet (POO) commence par organiser la cuisine en classifiant les objets que l'on utilise: on peut distinguer les ingrédients (que l'on consomme) et les ustensiles (que l'on réutilise). On constate que tous les ustensiles suivent un cycle commun utilisation-nettoyage-rangement à l'exception des cuisinières et autres micro-ondes qui ne passent pas au lave-vaisselle. Parmi les ustensiles, il y a des récipients, certains vont au feu, d'autres au micro-ondes et d'autres ne supportent pas d'être chauffés.

L'héritage n'est qu'une manière de décrire cette classification: une poêle à frire est un récipient qui va au feu, donc un récipient, donc un ustensile. Donc la classe poêle à frire hérite de la classe récipient qui hérite de la classe ustensile. 

Ensuite, on décrit dans les classes ce que les objets savent faire. Une cuisinière "sait" faire chauffer un récipient. Elle a donc une méthode faireChauffer qui prend comme paramètre un récipient qui va au feu (donc n'importe quel objet dont la classe hérite de la classe objetAllantauFeu). En petit-nègre informatique, cela donne dire à ma cuisinière de faireChauffer(maCasserole) ou traduit en anglais :

tell maCuisiniere to faireChauffer(maCasserole)

et si d'aventure on disait la même chose au lave-vaisselle, il répondrait : "L'objet lave-vaisselle ne comprend pas le message faireChauffer.".

La POO consiste à faire l'analyse d'un programme en termes d'objets manipulés et non de fonctions et de procédures à exécuter. C'est en s'efforçant de pratiquer cette analyse qu'on peut découvrir que les scripts-objets ont été conçus pour les gens qui suivent cette approche, et dans cette approche, leur usage est à la fois naturel et sans grand mystère.

Par contre si on reste dans l'approche classique d'une analyse procédurale, on éprouve les plus grandes difficultés à leur trouver une véritable utilité.

Laurent Sebilleau le 19/8/2004 sur applescript_fr@macplus.net 

4 déc. 2011

Finder vs System Events vs UNIX

tell application "Finder"
set List1 to the name of every item of startup disk
end tell

tell application "System Events"
set List2 to the name of every item of startup disk
end tell

set List3 to do shell script "ls -a /"

(count of List1) = (count of List2)
--> false

(count of List2) = (count of List3)
--> false

6 nov. 2011

The Zen of Python

set cmd to do shell script "python -c 'import this'"
display dialog cmd buttons {"•"} default button 1

>> Ouvrir le script dans l'éditeur

29 oct. 2011

Script To Markup Code

Crée le code des liens  de ce blog qui permettent d'ouvrir le script sur le Mac de l'utilisateur.

-- ASCII character (34) -- %22 -- quote
-- ASCII character (32) -- %20 -- espace
-- ASCII character (9) -- %09 -- tabulation
-- ASCII character (10) -- %0A --  retour chariot
-- ASCII character (58) -- %3A -- colon 
-- ASCII character (60) --  "%3C" -- <
-- ASCII character (62) --  %3E -- >

set charList to {ASCII character (32), ASCII character (34), ASCII character (9), ASCII character (58), ASCII character (62), ASCII character (60)}
set temp to the clipboard as text
set newText to ""

-- Codage des quote, espace, tabulation, colon, < et >
repeat with theChar in temp
if theChar is in charList then
set newText to newText & encodeChar(theChar)
else
set newText to newText & theChar
end if
end repeat

-- Codage des retour chariot
set theParagraphs to every paragraph of newText
set newContents to ""
repeat with thisParagraph in theParagraphs
set thisParagraph to thisParagraph & "%0A" as string
set newContents to newContents & thisParagraph as string
end repeat

-- Ecriture du lien HTML cliquable dans le presse-papiers

on encodeChar(this_char)
set the ASCII_num to (the ASCII number this_char)
set the hex_list to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
set x to item ((ASCII_num div 16) + 1) of the hex_list
set y to item ((ASCII_num mod 16) + 1) of the hex_list
return ("%" & x & y) as string
end encodeChar

iTunes Tag Concatenate

property separator : "+" -- Séparateur des Tags concaténés

tell application "iTunes"
activate
set theseTracks to selection of browser window 1 -- Liste des pistes selectionnées
end tell

-- Vérification sélection non vide
if theseTracks is {} then
beep
display dialog "Sélectionnez les pistes à modifier." buttons {"Annuler"} default button 1 with icon 0
return
end if

-- Sélection des Tags à concatener
set theChoice to choose from list
"1 | TITRE + ARTISTE | | Artiste de l'album | Album ", ¬
"2 | | TITRE + ARTISTE | Artiste de l'album | Album ", ¬
"3 | Titre | ARTISTE + ARTISTE DE L'ALBUM | |  Album", ¬
"4 | Titre | | ARTISTE + ARTISTE DE L'ALBUM | Album", ¬
"5 | Titre | Artiste | ARTISTE dE L'ALBUM + ALBUM ||", ¬
"6 | Titre | Artiste | | ARTISTE dE L'ALBUM + ALBUM", ¬
"7 | TITRE + ARTISTE DE L'ALBUM | Artiste | | Album", ¬
"8 | | Artiste | TITRE + ARTISTE DE L'ALBUM | Album", ¬
"9 | TITRE + ALBUM | Artiste | Artiste de l'album | |", ¬
"10 | | Artiste | Artiste de l'album | TITRE + ALBUM |", ¬
"11 | Titre | ARTISTE + ALBUM | Artiste de l'album | |", ¬
"12 | Titre | | Artiste de l'album | ARTISTE + ALBUM |"} with prompt "Sélectionner les Tags à concaténer :" & return & " | Titre | Artiste | Artiste de l'album | Album |"
if theChoice is false then error "Annulation Utilisateur" number -128

-- numéro de la ligne selectionnée dans la liste
set num to (first word of (theChoice as string)) as number

-- choix du séparateur de Tags
set separator to text returned of (display dialog "Choisisez un séparateur de Tags" default answer separator)

-- concaténation des Tags sélectionnés
tell application "iTunes"
set ofi to fixed indexing
set fixed indexing to true
with timeout of 9999 seconds
repeat with theTrack in theseTracks
if num = 1 then
tell theTrack to set name to (get name) & separator & (get artist)
tell theTrack to set artist to ""
else if num = 2 then
tell theTrack to set artist to (get name) & separator & (get artist)
tell theTrack to set name to ""
else if num = 3 then
tell theTrack to set artist to (get artist) & separator & (get album artist)
tell theTrack to set album artist to ""
else if num = 4 then
tell theTrack to set album artist to (get artist) & separator & (get album artist)
tell theTrack to set artist to ""
else if num = 5 then
tell theTrack to set album artist to (get album artist) & separator & (get album)
tell theTrack to set album to ""
else if num = 6 then
tell theTrack to set album to (get album artist) & separator & (get album)
tell theTrack to set album artist to ""
else if num = 7 then
tell theTrack to set name to (get name) & separator & (get album artist)
tell theTrack to set album artist to ""
else if num = 8 then
tell theTrack to set album artist to (get name) & separator & (get album artist)
tell theTrack to set name to ""
else if num = 9 then
tell theTrack to set name to (get name) & separator & (get album)
tell theTrack to set album to ""
else if num = 10 then
tell theTrack to set album to (get name) & separator & (get album)
tell theTrack to set name to ""
else if num = 11 then
tell theTrack to set artist to (get artist) & separator & (get album)
tell theTrack to set album to ""
else if num = 12 then
tell theTrack to set album to (get artist) & separator & (get album)
tell theTrack to set artist to ""
end if
end repeat
end timeout
set fixed indexing to ofi
end tell
display dialog "Opération terminée." buttons {"OK"} default button 1 with icon note giving up after 5