Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old May 23, 2009, 02:06 PM   #1
kidalex
macrumors newbie
 
Join Date: Sep 2008
Help with Sed and Applescript, replacing strings in files.

Here's what I'm trying to do:

I have a text document with html code in it which is a template for social bookmarking things on the bottom of posts on my blog.

The author of this template has two variables, xTITLEx, and xURIx. Supposedly, you're supposed to find and replace them with Textedit/Notepad., but I want to be able to do this quickly, using applescript.

I have gotten this far:
Code:
set titlequestion to display dialog "What is the Title?" default answer "" buttons {"Cancel", "Go..."} default button 2
set titleanswer to text returned of titlequestion
do shell script "sed 's/xTITLEx/" & titleanswer & "/g' /Users/Alexh/Documents/Personal/code/Social.txt > /Users/Alexh/Documents/Personal/code/SocialNew.txt"
set urlquestion to display dialog "What is the URL?" default answer "" buttons {"Cancel", "Go..."} default button 2
set urlanswer to text returned of urlquestion
do shell script "sed 's/xURIx/" & urlanswer & "/g' /Users/Alexh/Documents/Personal/code/SocialNew.txt > /Users/Alexh/Documents/Personal/code/SocialNew1.txt"
It works fine until the url question/answer part. The problem is that the command
Code:
sed 's/xURIx/urlanswer/g' file
replaces the string xURIx in the file 'file' with the 'urlanswer', right? So I plugged in the variable from the question, urlquestion, and here's the problem: the url is formatted like this: http://google.com, and sed recognizes it as another command (whenever there's '/', it think's its an option). Please help me to make http://google.com not considered part of the command, but a string!

Thank you!
kidalex is offline   0 Reply With Quote
Old May 23, 2009, 02:17 PM   #2
harry65
macrumors member
 
Join Date: Aug 2008
When using sed, you dont have to use '/' as the separator... you can use any character you want. I like to use @. So, instead of writing

Code:
sed 's/xURIx/" & urlanswer & "/g'
you can write

Code:
sed 's@xURIx@" & urlanswer & "@g'
(as long as 'urlanswer' does not contain an '@' character). I think this is much easier than trying to back-slash the '/' characters in urlanswer.
harry65 is offline   0 Reply With Quote
Old May 23, 2009, 02:20 PM   #3
kidalex
Thread Starter
macrumors newbie
 
Join Date: Sep 2008
Thank You

Thank you so much, very easy to understand answer!

kidalex is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Copy files to folder using Applescript Kitkat88 Mac Programming 7 Jun 23, 2014 08:45 AM
Finder extremely slow at replacing files LosLoco OS X Mavericks (10.9) 13 Mar 13, 2014 03:20 PM
Applescript moving files issue Liquid80 Mac Programming 2 Oct 3, 2013 12:29 AM
replacing lossless files with lossy files guitarmandp Mac Applications and Mac App Store 4 Apr 12, 2013 01:31 AM
Imovie 11 wont work after replacing files Tractor Torque Mac Basics and Help 1 Mar 27, 2013 03:51 PM

Forum Jump

All times are GMT -5. The time now is 12:42 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC