Make a simple script with OC under MAC

Source: Internet
Author: User

Let's start with the function of the footstep: Change the name of the. png image in the Mac Ducuments directory. Preparation: 1. Learn about making a script executable file ". sh" File 2. Learn about the use of the Nsfilemanager class under the OC language 3. Understanding Terminal compilation OC's. m file

Next is the method of production: 1. Create an OC Command line Tool Project

Write the features you need in the project: renaming the picture file:#import<Foundation/Foundation.h>

intMain (intargc,ConstChar* argv[]) {
@autoreleasepool {
Nsfilemanager*filemanager = [NsfilemanagerDefaultmanager];
//get the app hereDocumentsList of files and folders in the folder
Nsarray*documentpaths =Nssearchpathfordirectoriesindomains(nsdocumentdirectory,Nsuserdomainmask,YES);
NSString*documentdir = [DocumentpathsObjectatindex:0];
Nserror*error =Nil;
Nsarray*filelist = [[NsarrayAlloc]Init];
//filelistis an array containing the file name and folder name of all the files under the folder
FileList = [FileManagerContentsofdirectoryatpath:d OcumentdirError:&error];

//       The following code can list all the subfolder names in a given folder

Nsmutablearray*filearray = [[NsmutablearrayAlloc]Init];

//obtained in the above procedurefileListfolder names are listed in
for (NSString*fileinchfileList) {
NSString*path = [Documentdirstringbyappendingpathcomponent: file];
if([PathHassuffix:@ ". png"]) {
[FilearrayAddObject:p Ath];
}
}

NSLog(@ "Every Thing in the dir:%@", fileList);
NSLog(@ "All folders:%@", Filearray);

for (inti =0; I < Filearray.Count; i++) {
NSString*filepath = Filearray[i];
NSString*topath = [NSStringstringWithFormat:@ "%@/tree%d.png", documentdir,i+1];
[FileManagerCopyitematpath: FilePathTopath: TopathError:Nil];
}

}
return0;}
2. The next step is to compile the. m file under TerminalOpen the terminal CD to the. m file directory Run command: gcc-framework foundation-o myscript main.m you'll get a myscript executable file.
3. Make executable script file. sh file

To open the Terminal input command:CD Documentsmkdir MyScriptVim changeimgname.shEnter edit mode, input I for editingedit content as:#!/bin/bash./myscript
then press Control+c to exit Edit status input: Wq keep exitingcommand input: chmod +x./changeimgname.shcomplete the critical step and continue looking down:4. Copy the MyScript executable file created by step 2 to the ~/documents/MyScript directory, which is the sibling of the changeimgname.sh file
5. Start experimenting:The CD -to-changeimgname.sh directory is: ~/documents/MyScript (in fact, it is now in this directory) Enter the command.changeimgname.sh




How is not the reaction, no response on the right, now is a step forward to the ~/documents directory of a few. png format pictures, then you can find the ~/documents directory under a few different names of the pictures, really effective oh, come on try it



Make a simple script with OC under MAC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.