Last active
July 19, 2016 15:42
-
-
Save kejadlen/63af13979e10cd319b2c74c96d40ccfd to your computer and use it in GitHub Desktop.
Cleaning Alfred workflows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'tempfile' | |
plist = ARGF.read | |
Tempfile.create('info.plist') do |f| | |
f << plist | |
f.flush | |
vars = `/usr/libexec/PlistBuddy -c "Print :variablesdontexport" #{f.path}` | |
vars = vars.split("\n")[1..-2].map(&:strip) | |
vars.each do |var| | |
`/usr/libexec/PlistBuddy -c "Set :variables:#{var} ''" #{f.path}` | |
end | |
f.rewind | |
puts f.read | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate plist; | |
use plist::Plist; | |
use std::collections::btree_map::BTreeMap; | |
use std::io::{self, Read}; | |
fn main() { | |
let mut buffer = String::new(); | |
io::stdin().read_to_string(&mut buffer).unwrap(); | |
let cursor = io::Cursor::new(buffer); | |
let mut plist = Plist::read(cursor).unwrap(); | |
clean_vars(&mut plist); | |
let mut event_writer = plist::xml::EventWriter::new(io::stdout()); | |
for event in plist.into_events() { | |
event_writer.write(&event).unwrap(); | |
} | |
} | |
fn clean_vars(plist: &mut Plist) { | |
let mut dict = plist.as_dictionary_mut().unwrap(); | |
let vars = variables_dont_export(&dict); | |
let mut variables = dict.get_mut("variables") | |
.and_then(|x| x.as_dictionary_mut()) | |
.unwrap(); | |
for var in vars { | |
variables.insert(var.clone(), Plist::String("".into())); | |
} | |
} | |
fn variables_dont_export(dict: &BTreeMap<String, Plist>) -> Vec<String> { | |
dict.get("variablesdontexport") | |
.and_then(|x| x.as_array()) | |
.map(|x| { | |
x.iter() | |
.map(|x| x.as_string().unwrap().into()) | |
.collect::<Vec<_>>() | |
}) | |
.unwrap() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment