La lecture en ligne est gratuite
Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres
Télécharger Lire

Automator AppleScript Actions Tutorial

De
50 pages
AutomatorAppleScriptActionsTutorial
AppleApplications>Automator
2007-07-18 IMPLIED,WITHRESPECTTOTHIS
DOCUMENT,ITSQUALITY,ACCURACY,
AppleInc. MERCHANTABILITY,ORFITNESSFORA
PARTICULARPURPOSE.ASARESULT,THIS©2005,2007AppleInc.ISPROVIDED“ASIS,”AND
Allrightsreserved. YOU,THEREADER,AREASSUMINGTHE
ENTIRERISKASTOITSQUALITYAND
ACCURACY.Nopartofthispublicationmaybe
INNOEVENTWILLAPPLEBELIABLEFORreproduced,storedinaretrievalsystem,or
DIRECT,INDIRECT,SPECIAL,INCIDENTAL,
transmitted,inanyformorbyanymeans, ORCONSEQUENTIALDAMAGES
RESULTINGFROMANYDEFECTORmechanical,electronic,photocopying,
INACCURACYINTHISDOCUMENT,evenif
recording,orotherwise,withoutprior advisedofthepossibilityofsuchdamages.
writtenpermissionofAppleInc.,withthe THEWARRANTYANDREMEDIESSET
followingexceptions:Anypersonishereby FORTHABOVEAREEXCLUSIVEANDIN
LIEUOFALLOTHERS,ORALORWRITTEN,authorizedtostoredocumentationona
EXPRESSORIMPLIED.NoAppledealer,agent,
singlecomputerforpersonaluseonlyand oremployeeisauthorizedtomakeany
modification,extension,oradditiontothistoprintcopiesofdocumentationfor
warranty.
personaluseprovidedthatthe
SomestatesdonotallowtheexclusionordocumentationcontainsApple’scopyright limitationofimpliedwarrantiesorliabilityfor
notice. incidentalorconsequentialdamages,sothe
abovelimitationorexclusionmaynotapplyto
TheApplelogoisatrademarkofAppleInc. you.Thiswarrantygivesyouspecificlegal
rights,andyoumayalsohaveotherrightswhich
Useofthe“keyboard”Applelogo varyfromstatetostate ...
Voir plus Voir moins
AutomatorAppleScriptAppleApplications>Automator2007-07-18ActionsTutorial
IMPLIED,WITHRESPECTTOTHISDOCUMENT,ITSQUALITY,ACCURACY,AppleInc.MERCHANTABILITY,ORFITNESSFORA©2005,2007AppleInc.PARTICULARPURPOSE.ASARESULT,THISDOCUMENTISPROVIDEDASIS,ANDAllrightsreserved.YOU,THEREADER,AREASSUMINGTHEENTIRERISKASTOITSQUALITYANDNopartofthispublicationmaybeACCURACY.reproduced,storedinaretrievalsystem,orINNOEVENTWILLAPPLEBELIABLEFORDIRECT,INDIRECT,SPECIAL,INCIDENTAL,transmitted,inanyformorbyanymeans,ORCONSEQUENTIALDAMAGESmechanical,electronic,photocopying,RESULTINGFROMANYDEFECTORINACCURACYINTHISDOCUMENT,evenifrecording,orotherwise,withoutprioradvisedofthepossibilityofsuchdamages.writtenpermissionofAppleInc.,withtheTHEWARRANTYANDREMEDIESSETfollowingexceptions:AnypersonisherebyFORTHABOVEAREEXCLUSIVEANDINauthorizedtostoredocumentationonaLIEUOFALLOTHERS,ORALORWRITTEN,EXPRESSORIMPLIED.NoAppledealer,agent,singlecomputerforpersonaluseonlyandoremployeeisauthorizedtomakeanytoprintcopiesofdocumentationformodification,extension,oradditiontothispersonaluseprovidedthatthewarranty.SomestatesdonotallowtheexclusionordocumentationcontainsApplescopyrightlimitationofimpliedwarrantiesorliabilityfornotice.incidentalorconsequentialdamages,sotheabovelimitationorexclusionmaynotapplytoTheApplelogoisatrademarkofAppleInc.you.Thiswarrantygivesyouspecificlegalrights,andyoumayalsohaveotherrightswhichUseofthekeyboardApplelogovaryfromstatetostate.(Option-Shift-K)forcommercialpurposeswithoutthepriorwrittenconsentofApplemayconstitutetrademarkinfringementandunfaircompetitioninviolationoffederalandstatelaws.Nolicenses,expressorimplied,aregrantedwithrespecttoanyofthetechnologydescribedinthisdocument.Appleretainsallintellectualpropertyrightsassociatedwiththetechnologydescribedinthisdocument.ThisdocumentisintendedtoassistapplicationdeveloperstodevelopapplicationsonlyforApple-labeledcomputers.Everyefforthasbeenmadetoensurethattheinformationinthisdocumentisaccurate.Appleisnotresponsiblefortypographicalerrors.AppleInc.1InfiniteLoopCupertino,CA95014408-996-1010Apple,theApplelogo,AppleScript,AppleScriptStudio,Cocoa,iPhoto,iPod,iTunes,Mac,MacOS,Objective-C,andXcodearetrademarksofAppleInc.,registeredintheUnitedStatesandothercountries.FinderisatrademarkofAppleInc.SimultaneouslypublishedintheUnitedStatesandCanada.EventhoughApplehasreviewedthisdocument,APPLEMAKESNOWARRANTYORREPRESENTATION,EITHEREXPRESSOR
ContentsIntroductionChapter1Chapter2Chapter3Chapter4Chapter5Chapter6IntroductiontoAutomatorAppleScriptActionsTutorial7OrganizationofThisDocument7BeforeYouStart9WhatIsanAutomatorAction?9TheActionYouWillCreate11CreatingtheProject13CreatingtheUserInterface19OpeningtheActionNibFile19PlacingandConfiguringUser-InterfaceObjects20EstablishingBindings27BindingsinanAction27EstablishingtheBindingsoftheAction28AlternativestoBindings31ConfiguringtheAction33EditingtheInformationPropertyList33ActionInputandOutput35DefaultParameterSettings37TheActionDescription38OtherSettings39WritingtheActionScript41TheonrunCommandHandler41WritingtheSubroutines432007-07-18|©2005,2007AppleInc.AllRightsReserved.3
Chapter74CONTENTSBuildingandTestingtheActionDocumentRevisionHistory2007-07-18|9445©2005,2007AppleInc.AllRightsReserved.
Chapter1Chapter2Chapter3Chapter4Chapter5FiguresBeforeYouStart9Figure1-1TheFindMessagesinMailaction9Figure1-2TheCopyUnreadMailtoiPodNotesworkflow10Figure1-3ThePassRandomItemsactioninaworkflow11CreatingtheProject13Figure2-1SelectingtheAppleScriptactionprojecttemplate14Figure2-2Specifyingprojectnameandlocation15Figure2-3ThefilesofanAppleScriptactionprojectinXcode16CreatingtheUserInterface19Figure3-1Thenibfilewindow,initialactionview,andpalette19Figure3-2Automatoricon20Figure3-3Placingatextfieldbydraggingitfromthepalette21Figure3-4Settingthesizeattributeofthetextfield22Figure3-5Resizingthetextfield23Figure3-6Changingthestringinainanon-editabletextfield(alabel)23Figure3-7Changingthenumberofradiobuttonsinamatrix(radiobuttons)24Figure3-8Changingthetitleofabutton24Figure3-9FinaluserinterfaceofPassRandomItemsaction25EstablishingBindings27Figure4-1Bindingbetweenthepop-uplistandapropertyoftheparameterdictionary82Figure4-2AddingkeysasattributesoftheParametersinstance.29Figure4-3Bindingbetweenthecontrollerandtheparametersdictionary30Figure4-4TheselectedIndexattributeinthebindingsinspector31ConfiguringtheAction33Figure5-1ThepropertyinspectorforAutomatoractions(generalcollection)34Figure5-2ThecompletedGeneralcollectionofAutomatorproperties)35Figure5-3AutomatorpropertyinspectorInputcollection36Figure5-4ThedefaultparametersforthePassRandomItemsaction382007-07-18|©2005,2007AppleInc.AllRightsReserved.5
Chapter6Chapter76FIGURESFigure5-5ThedescriptionforthePassRandomItemsaction38WritingtheActionScript41Figure6-1Thetemplatefortheonrunhandler41Figure6-2Initializinglocaloutputandparametervariables42Figure6-3Thefinalonrunhandler42Figure6-4Subroutinescalledbythemainscript43BuildingandTestingtheAction45Figure7-1Figure7-2Figure7-3ExecutablesettingsforthePassRandomItemsaction45TestingthePassRandomItemsactioninaworkflow46TheAppleScriptdebugger472007-07-18|©2005,2007AppleInc.AllRightsReserved.
INTRODUCTIONIntroductiontoAutomatorAppleScriptActionsTutorialThistutorialshowsyouhowtocreateanactionfortheAutomatorapplicationusingAppleScriptastheimplementationlanguage.Important:SomeoftheXcodefeaturesmentionedinthistutorial,suchastheAppleScriptdebuggerandthepropertyinspector,wereintroducedwithXcode2.1.IfyouhaveanearlierversionofXcode,youdonothaveaccesstothesefeatures.ThetutorialassumesthatyouarefamiliarwithAppleScript,butotherwisehasnootherprerequisites.Itishelpful,however,ifyouhavesomeexperiencewithAppleScriptStudio.OrganizationofThisDocumentThistutorialhasthefollowingchapters,whicharemeanttobereadinthegivenorder:1.BeforeYouStart(page9)givesanoverviewofAutomatoractionsandworkflows.Italsodescribestheactionthatyouwillcreateinthetutorial.2.CreatingtheProject(page13)explainshowtocreateanAppleScriptactionprojectandidentifiesthekeyelementsofsuchprojects.3.CreatingtheUserInterface(page19)showsyouhowtocreatetheuserinterfaceoftheactionusingtheInterfaceBuilderapplication.4.EstablishingBindings(page27)explainswhatCocoabindingsareanddescribeshowyouestablishbindingsfortheaction.5.ConfiguringtheAction(page33)discusseshowtosetthepropertiesoftheactioninitsInfo.plistfile.6.WritingtheActionScript(page41)showsthescriptthatyouwritefortheactionandexplainsthegeneralstructureandbehaviorofallsuchscripts.7.BuildingandTestingtheAction(page45)describestechniquesfortestinganddebugginganactionafteritisbuilt.OrganizationofThisDocument2007-07-18|©2005,2007AppleInc.AllRightsReserved.7
8INTRODUCTIONIntroductiontoAutomatorAppleScriptActionsTutorialOrganizationofThisDocument2007-07-18|©2005,2007AppleInc.AllRightsReserved.
CHAPTER1BeforeYouStartInthistutorialyouaregoingtolearnthebasicstepsforconstructinganAutomatoractionusingAppleScriptasthedevelopmentlanguage.Intheprocessoflearning,youwillcreateaactionthatyoucanproductivelyuseinworkflows.Butbeforeyoubegin,letstakeamomenttoreviewwhatanactionisandtolookattheactionyouwillcreate.WhatIsanAutomatorAction?Mostpeoplearefamiliarwiththenotionofbuildingblocks.Byplacingsmallbutwell-definedunitsincertainrelationshipswitheachother,onecancomposecomplexandevenelegantstructures.AnAutomatoractionissuchabuildingblock.Anactionisasmall,discretefunctionalunit;itperformsawell-definedoperationusuallyondataofaspecifictype,suchascopyingafileoraddingphotostoaniPhotoalbum.Itoftenofferstheuserasimpleuserinterfaceforsettingcertainparametersoftheoperation.Forexample,theactioninFigure1-1selectscertainMailmessagesbasedonspecifiedcriteria.Figure1-1TheFindMessagesinMailactionByitself,anactioncannotdomuch.Foronething,itrequirestheAutomatorapplicationtoprovidethecontextforitsexecution.But,moreimportantly,anactionsverydiscretenesslimitsitsusefulness;anactionisdesignedtocompleteasmall,well-definedtask,andnothingmore.Tobeeffective,anactionmustbeplacedinameaningfulsequenceofotheractions.Thissequenceofactionsiscalledaworkflow.Inaworkflowtheoutputofoneactionisusually(butnotalways)passedtothenextactionintheworkflowasinput.Automatororchestratesthisprocessbystartingeachactioninturnandpassingittheoutputofthepreviousaction.AworkflowexpressesaoperationthatcanbearbitrarilyWhatIsanAutomatorAction?2007-07-18|©2005,2007AppleInc.AllRightsReserved.9
10CHAPTER1BeforeYouStartcomplex,andthefinalproductofthatoperationisusuallytheoutputofthelastaction.Forexample,theworkflowinFigure1-2getsausersunreadmailanddownloadsthemessagestotheNotessectionofaconnectediPod.Figure1-2TheCopyUnreadMailtoiPodNotesworkflowAsanAutomatorworkflow(suchastheoneabove)illustrates,anactionisusuallydesignedtoacceptinputandproduceoutputofspecificdatatypes(althoughsomeactionswilltakeandprovideanytypeofdata).Thussomeactionsmaybeincompatiblewithotheractions;theCombineMailMessagesaction,forinstance,couldnotacceptAddressBookdata.Buttherecanbewhatarecalledconversionactionstobridgebetweenactionswithincompatibletypesofdata.Fromadevelopmentperspective,anactionisaloadablebundleinstalledinoneoffourlocations: /System/Library/Automator(Apple-providedactions) /Library/Automator(third-partyactions,generalaccess) ~/Library/Automator(per-useraccess) Insideanapplicationbundle(accessdeterminedbyaccesstoapplication)WhatIsanAutomatorAction?2007-07-18|©2005,2007AppleInc.AllRightsReserved.
CHAPTER1BeforeYouStartTheactionbundlecancontainexecutablecode,AppleScriptscripts,shellscripts,andlocalizedstrings,nibfiles,andotherresources.WhenAutomatorislaunched,theapplicationextractsconfigurationinformationfromtheactionbundlesanddisplayssomeofthisinformationinitsuserinterface.Italsoloadsthebundlesoftheactionsplacedinaworkflowatsomepointbeforetheexecutionoftheworkflow(theexactmomentdiffersforactionsbasedonObjective-CandactionsbasedonAppleScript).ForacompletedescriptionofthearchitectureofAutomatoractionsandworkflows,seeHowAutomatorWorksintheAutomatorProgrammingGuide.TheActionYouWillCreateInthistutorialyouwillcreateanactionnamedPassRandomItems.Theactionacceptsalistofitems(ofanytype)fromthepreviousactionandpassesarandomsubsetofthoseitemstothenextaction.Userscanspecifythenumberorpercentageofitemstopassintheactionsuserinterface.Figure1-3showsthePassRandomItemsactioninaworkflow.Figure1-3ThePassRandomItemsactioninaworkflowNote:TheprojectforthePassRandomItemsactionisinstalledasanexample(underthenameRandomizer)in/Developer/Examples/Automator.Inthisworkflow,theFilterPhotosiniPhotoactionpassesallselectedphotostakenwithinthelasttwomonthstoPassRandomItems.Thisaction,inturn,passes20randomphotosfromthatinitialselectiontoanactionthatplaystheminaslideshow.Afteryoucompletethistutorialandbeforeyouattemptdevelopinganyactiononyourown,youshouldtaketimetoconsiderthedesignoftheaction.ReadDesignGuidelinesforActionsinAutomatorProgrammingGuideforasummaryofpertinentdesignguidelines.TheActionYouWillCreate2007-07-18|©2005,2007AppleInc.AllRightsReserved.11