Action

Shorten Text

Posted by Chris Grieser, Last update 27 days ago

UPDATES

27 days ago

added info-message with the number of characters saved

Several automatic replacements to shorten the length for a tweet, like “with” ➞ “w/” or “something” ➞ “sth”

Useful for drafting tweets where every character counts.

Contains common abbreviations for Englich and German.
Submit your own suggestions (or additional languages) via pull request at the Github project for the Twitter Workspace

Steps

  • script

    var replacementURL = 'https://raw.githubusercontent.com/chrisgrieser/twitter-workspace-for-drafts/main/shorten_list.csv';
    var delimiter = ','; //delimiter used in the list
    //======================================================
    
    //saves draft to be able to undo this action
    draft.saveVersion(); 
    previousLength = draft.content.length;
    
    //fetch csv-list online
    var http = HTTP.create(); //s explanatin: https://scripting.getdrafts.com/classes/http#request
    var response = http.request({"url": replacementURL, "method": "GET"});
    
    if (response.success) {
      var csvText = response.responseText;
    }
    else {
      console.log(response.statusCode);
      console.log(response.error);
    }
    
    //filtering of '" (has only be in the csv for convenince making spaces easier visible)
    csvText = csvText.replaceAll ("\"","");
    
    //filtering of empty and JS-style comments in the fetched csv
    csvText = csvText.replace (/\/\/.*?$/gm,"");
    while (csvText.includes("\n\n") ){
    	csvText = csvText.replaceAll ("\n\n","\n");
    }
    
    csvText = csvText.substring(1,csvText.length-1); //removes empty first line
    
    //removes double spaces
    csvText = csvText.replaceAll ("  "," ");
    
    //every line becomes an element of the array
    var replacementList = csvText.split("\n"); 
    
    //replaces longform with shortform
    replacementList.forEach (element => {
    	temp = element.split(delimiter);
    	draft.content = draft.content.replaceAll (temp[0],temp[1]);
    } )
    
    //display the number of characters saved
    charsSaved = previousLength - draft.content.length;
    if (charsSaved > 0){
    	app.displaySuccessMessage (charsSaved.toString() + " chars saved");
    } else {
    	app.displayErrorMessage("Nothing found that can be shortened.");
    }
    
    draft.update();
    
    
    

Options

  • After Success Default
    Notification Error
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.